Home > DeveloperSection > Forums > Issue with Regex Replace
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    December-27-2013 6:58 AM

 C# C# 
Ratings:


 1 Reply(s)
 1118  View(s)
Rate this:

I am trying to replace every occurrence of "]}]" (ignore the quotes) using the Regex object in C#.

I have set up the Regex Escape value like this:

var regexReplaceVar = new Regex(Regex.Escape("]}]"));

I then call the replace method as shown below:

myEditedString = regexReplaceVar.Replace(startString, sringToInsert, 1);

It does not appear to be working the way I think it would. Are the characters I am attempting to replace special to Regular Expressions? Should they be modified to have them be taken as literals?

Thanks in advance.



Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    December-27-2013 7:41 AM

Hi Jeet,

All of the characters you are seeking must be escaped in C# Regex.

Regex:

\]\}\]

C#:

Regex regexObj = new Regex(@"\]\}\]", RegexOptions.IgnoreCase);

string result = regexObj.Replace(subjectString, replaceWith);

Console.WriteLine(result);

result holds the replaced string, this is important because subjectString doesn't change.


Don't want to miss updates? Please click the below button!

Follow MindStick