RandyWhirl
RandyWhirl

Reputation: 103

Regex.Replace not working as expected with Regexoptions.IgnoreCase

If I have a few strings:

str = "This is a cool string (orch. details here): 2. Denied."
str2 = " (Orch. details here)" <--notice the capital letter

and then I do this line of code to try to clear that part out:

str3 = Regex.Replace(str, str2, "", RegexOptions.IgnoreCase);

str3 ends up being exactly as what str was before execution. I suspect that it is not "seeing" the other characters such as the periods because when I do this with just simple alphabetic characters in str and str2 then it will replace it just fine.

What gives?! :)

Thank you for any insight!

Upvotes: 1

Views: 531

Answers (1)

JaredPar
JaredPar

Reputation: 755397

Both parens and periods have meanings in regular expressions. Hence it's not trying to literally match the contents of str2 but instead a regular expression that is defined by str2. If you want it to match literally you need to escape the string

str2 = Regex.Escape(str2);

Upvotes: 5

Related Questions