开发者

Regex.Replace why does \b prevent this?

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 | Rege开发者_开发百科xOptions.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);


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"


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

@"\bwhere\b"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜