1

I am having a strange issue with Regex.Replace

string test = "if the other party is in material breach of such contract and, after <span style=\"background-color:#ffff00;\">forty-five (45)</span> calendar days notice of such breach";
string final = Regex.Replace(test, "forty-five (45)", "forty-five (46)", RegexOptions.IgnoreCase);

the "final" string still shows "forty-five (45)". Any idea why? I am assuming it has to do something with the tag. How do I fix this?

Thanks

6
  • 2
    What language is this in? Commented Feb 5, 2014 at 0:28
  • Did you really want to replace forty-five (45) with forty-five (46)? Looks like a typo. Commented Feb 5, 2014 at 0:36
  • I am assuming it is because of the special characters "(" and ")" Commented Feb 5, 2014 at 0:36
  • @ErikPhilips, this is just a test text. I know the text does not make sense but this is just a test scenario which is failing. Commented Feb 5, 2014 at 0:37
  • 1
    Don't forget the dangers of parsing html with regex Commented Feb 5, 2014 at 1:19

3 Answers 3

6

Escape the parenthesis. Depending on the language, might require two back slashes.

string final = Regex.Replace(test, "forty-five \(45\)", "forty-five (46)", RegexOptions.IgnoreCase);

Basically, parenthesis are defined to mean something, and by escaping the characters, you are telling regex to use the parenthesis character, and not the meaning.

Better yet, why are you using a Regex to do this at all? Try just doing a normal string replacement.

string final = test.Replace("forty-five (45)", "forty-six (46)")
Sign up to request clarification or add additional context in comments.

5 Comments

I actually simplified the code for posting. The "search" and "replace" variables are dynamic. So I can't really escape anything.
Why can't you escape ( and )? Those can be string replaced before regex replaced.
@ErikPhilips the actual string might contain other special characters as well. So it won't be feasible to check on each special character.
If that's the case, you probably want to do a replacement that doesn't involve regex. See my edit.
It seems you don't have a clear understanding (or you haven't communicated it) of what your source and replacement values are. Escaping special characters isn't that hard, I don't see the issue.
1

Parentheses are special in regular expressions. They delimit a group, to allow for things such as alternation. For example, the regular expression foo(bar|bat)baz matches:

  • foo, followed by
  • either bar OR bat, followed by
  • baz

So, a regular expression like foo(bar) will never match the literal string foo(bar). What it will match is the literal string foobar. Consequently, you need to escape the metacharacters. In C#, this should do you:

string final = Regex.Replace(test, @"forty-five \(45\)", "forty-five (46)", RegexOptions.IgnoreCase);

The @-quoted string helps avoid headaches from excessive backslashes. Without it, you'd have to write "forty-five \(45\)".

Comments

0

If you are unable to escape the parenthesis, put them in a character class:

forty-five [(]45[)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.