到目前为止,我有一个正则表达式可以满足我的所有要求,突然我得到一个字符串,它具有保留字符,例如 C++ 中的 + 和 C# 中的 #。下面的代码适用于我的所有单词集合,除了 C++ 和 C#
MatchCollection matches= Regex.Matches(@"This program is written in C# We'll delete it after ten days", @"\bC\+\+\b");
foreach(Match m in matches)
{
Console.Write(m.Value);
}
谁能指出原因?
请您参考如下方法:
你应该在第二个边界使用 \B 而不是 \b
MatchCollection matches= Regex.Matches(@"This program is written in C# We'll delete it after ten days", @"\bC\#\B");
您可以阅读以下链接了解更多信息: http://www.regular-expressions.info/wordboundaries.html
