A string replace function with support of custom wildcards and escaping these wildcards in C#


I need to write a string replace function with custom wildcards support. I also should be able to escape these wildcards. I currently have a wildcard class with Usage, Value and Escape properties.

So let's say I have a global list called <strong>Wildcards</strong>. Wildcards has only one member added here:

Wildcards.Add(new Wildcard { Usage = @"\Break", Value = Enviorement.NewLine, Escape = @"\\Break" });

So I need a CustomReplace method to do the trick. I should replace the specified parameter in a given string with another one just like the string.Replace. The only difference here that it must use my custom wildcards.

string test = CustomReplace("Hi there! What's up?", "! ", "!\\Break"); // Value of the test variable should be: "Hi there!\r\nWhat's up?" // Because \Break is specified in a custom wildcard in Wildcards // But if I use the value of the wildcard's Escape member, // it should be replaced with the value of Usage member. test = CustomReplace("Hi there! What's up?", "! ", "!\\\\Break"); // Value of the test variable should be: "Hi there!\\BreakWhat's up?"

My current method doesn't support escape strings. It also can't be good when it comes to performance since I call string.Replace two times and each one searches the whole string, I guess.

// My current method. Has no support for escape strings. CustomReplace(string text, string oldValue, string newValue) { string done = text.Replace(oldValue, newValue); foreach (Wildcard wildcard in Wildcards) { // Doing this: // done = done.Replace(wildcard.Escape, wildcard.Usage); // ...would cause trouble when Escape contains Usage. done = done.Replace(wildcard.Usage, wildcard.Value); } return done; }

So, do I have to write a replace method which searches the string char by char with the logic to find and seperate both Usage and Escape values, then replace Escape with Usage while replacing Usage with another given string?

Or do you know an already written one?

Can I use regular expressions in this scenerio?

If I can, how? (Have no experience in this, a pattern would be nice)

If I do, would it be faster or slower than char by char searching?

Sorry for the long post, I tried to keep it clear and sorry for any typos and such; it's not my primary language. Thanks in advance.


You can try this:

public string CustomReplace(string text, string oldValue, string newValue) { string done = text.Replace(oldValue, newValue); var builder = new StringBuilder(); foreach (var wildcard in Wildcards) { builder.AppendFormat("({0}|{1})|", Regex.Escape(wildcard.Usage), Regex.Escape(wildcard.Escape)); } builder.Length = builder.Length - 1; // Remove the last '|' character return Regex.Replace(done, builder.ToString(), WildcardEvaluator); } private string WildcardEvaluator(Match match) { var wildcard = Wildcards.Find(w => w.Usage == match.Value); if (wildcard != null) return wildcard.Value; else return match.Value; }

I think this is the easiest and fastest solution as there is only one Replace method call for all wildcards.


So if you are happy to just use Regex to fulfil your needs then you should <a href="http://www.regular-expressions.info/dotnet.html" rel="nofollow">check out this link</a>. It has some great info for using in .Net. The website also has loads of examples on who to construct Regex patterns for many different needs.

A basic example of a Replace on a string with wildcards might look like this...

string input = "my first regex replace"; string result = System.Text.RegularExpressions.Regex.Replace(input, "rep...e", "result"); //result is now "my first regex result"

notice how the second argument in the Replace function takes a regex pattern string. In this case, the dots are acting as a wildcard character, they basically mean "match any single character"

Hopefully this will help you get what you need.


Why dont you use the regular expressions INSTEAD of your wildcards?

It's ready made :)

<a href="http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx" rel="nofollow">http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx</a>

<a href="http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx" rel="nofollow">http://www.c-sharpcorner.com/UploadFile/prasad_1/RegExpPSD12062005021717AM/RegExpPSD.aspx</a>

<a href="http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx</a>


If you define a pattern for both your wildcard and your escape method, you can create a Regex which will find all the wildcards in your text. You can then use a MatchEvaluator to replace them.

class Program { static Dictionary<string, string> replacements = new Dictionary<string, string>(); static void Main(string[] args) { replacements.Add("\\Break", Environment.NewLine); string template = @"This is an \\Break escaped newline and this should \Break contain a newline."; // (?<=($|[^\\])(\\\\){0,}) will handle double escaped items string outcome = Regex.Replace(template, @"(?<=($|[^\\])(\\\\){0,})\\\w+\b", ReplaceMethod); } public static string ReplaceMethod(Match m) { string replacement = null; if (replacements.TryGetValue(m.Value, out replacement)) { return replacement; } else { //return string.Empty? //throw new FormatException()? return m.Value; } } }


  • QuickBook Online Reconnect & expire Issue
  • Object initializing with db null check
  • SigninwithIntuit with multiple user of the same QuickBook Company
  • Switch between two beans in spring
  • Mixing PDO and MySQL Functions?
  • Insert paragraph between text of richtextbox and change font and color of added paragraph
  • Get XML response value with GDataXML
  • opengl window freezing during move/resize
  • Rails redirect_to another controller method throwing “Template missing”
  • Mysql EAV match row as field or entity's property value
  • Save website uploads in a subdomain
  • Detect language of Word document
  • How to access recipient on sent messages page with mailboxer
  • Distributed JMS based logging .. falling flat?
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • matching similar elements in between two lists
  • Prolog Ambiguous Output
  • Mocha throws unexpected token error for ES6 object spread operator
  • IE10 strips out hashtag from the URL
  • Create a link to a web page that runs a Javascript function on the page
  • Sensibility of combined Maven/Ant+Ivy build management for dual platform Desktop/Android deployment?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Why use database factory in asp.net mvc?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Ensure fsync did its job
  • Functions in global context
  • WinForms: two way TextBox problem
  • Join two tables and save into third-sql
  • Convert array of 8 bytes to signed long in C++
  • R: gsub and capture
  • Acquiring multiple attributes from .xml file in c#
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Understanding cpu registers
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • How can i traverse a binary tree from right to left in java?
  • Why do underscore prefixed variables exist?