简体   繁体   English

带有单词边界的正则表达式不匹配

[英]Regular expression with word boundaries is not matching

I am trying to compute a regular expression to use with TFS Power Tools with the DocumentWell feature. 我正在尝试使用DocumentWell功能计算与TFS Power Tools一起使用的正则表达式。

I am testing this in a console application at the moment. 我目前正在控制台应用程序中对此进行测试。

Console.WriteLine(Regex.IsMatch(@"C:\User\User\My Documents\Visual Studio 2010\Project\", "\bProject\b"));

This is what I have tried (output is "False", so my regex string is \\bProject\\b . I have followed through the following link: 这是我试过的(输出是“假”,所以我的正则表达式字符串是\\bProject\\b 。我已经通过以下链接:

http://www.regular-expressions.info/wordboundaries.html http://www.regular-expressions.info/wordboundaries.html

Which I thought that I understood... I really struggle with regex so could somebody help me out with this regex and explain what I am doing wrong? 我认为我明白了...我真的很难与正则表达式斗争所以有人可以帮我解决这个正则表达式并解释我做错了什么?

Use also a verbatim string for the regex, see String literals on msdn 也可以使用regex的逐字字符串,请参阅msdn上的字符串文字

Console.WriteLine(Regex.IsMatch(@"C:\User\User\My Documents\Visual Studio 2010\Project\", @"\bProject\b"));

otherwise you have to escape twice 否则你必须逃脱两次

Console.WriteLine(Regex.IsMatch(@"C:\User\User\My Documents\Visual Studio 2010\Project\", "\\bProject\\b"));

See the difference of regular and verbatim string 查看常规字符串和逐字字符串的区别

string input = @"C:\User\User\My Documents\Visual Studio 2010\Project\";

string reg = "\bProject\b";
string regVerbatim = @"\bProject\b";

Regex r = new Regex(reg);
Regex rVerbatim = new Regex(regVerbatim);

Console.Write("Regular String regex: " + r.ToString() + " isMatch :");
Console.WriteLine(r.IsMatch(input));
Console.Write("Verbatim String regex: " + rVerbatim.ToString() + " isMatch :");
Console.WriteLine(rVerbatim.IsMatch(input));

Output: 输出:

Regular String regex:Projec isMatch :False 常规字符串正则表达式:Projec isMatch:False
Verbatim String regex: \\bProject\\b isMatch :True Verbatim String regex:\\ bProject \\ b isMatch:True

In the regular string the last "t" of the Regex is deleted and also the empty string before the word, that is because the string interpreted \\b as backspace and doesn't hand it over to the regex interpreter. 在常规字符串中,正则表达式的最后一个“t”被删除,并且在单词之前也是空字符串,这是因为字符串将\\b解释为退格并且不将其交给正则表达式解释器。

So either escape the backslash so that from \\\\bProject\\\\b \\bProject\\b is handed to the regex interpreter, or use a verbatim string, so that the string doesn't interprets the \\b . 因此要么转义反斜杠,以便从\\\\bProject\\\\b \\bProject\\b传递给正则表达式解释器,或者使用逐字字符串,这样字符串就不会解释\\b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM