tags:

views:

34

answers:

2

Why does the second statement fail?

works

Regex.Replace("zz WHERE zz", "where", "yy", RegexOptions.IgnoreCase | RegexOptions.Singleline);

does not

Regex.Replace("zz WHERE zz", "\bwhere\b", "yy", RegexOptions.IgnoreCase | RegexOptions.Singleline);

This works to but replaces the space which i do not want to do

Regex.Replace("zz WHERE zz", " where ", "yy", RegexOptions.IgnoreCase | RegexOptions.Singleline);
+7  A: 

Because \b is the backspace control character (U+0008). The backslashes themselves there don't even get to the regular expression.

To use it as intended in a regular expression you need to either double-escape (escape the backslashes for C#'s string so they are normal backslashes for the regex):

"\\bwhere\\b"

or use a verbatim string literal:

@"\bwhere\b"
Joey
+2  A: 

You need to escape the backslashes in C#, or else use a verbatim string literal @:

@"\bwhere\b"
Mark Byers