我正在尝试使用正则表达式匹配一个精确的字符串(我不能使用字符串比较,所以请不要这样建议)。
string regexValue = "CB/TF8/C9";
bool test = Regex.IsMatch(
string.Join("\r\n", "This is the description CB/TTF8/C9 and this is more description CB/TF8/C9", "CCB/TF8/C9"),
regexValue ,
RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
);我希望与CB/TF8/C9完全匹配,这样它就不应该与CB/TTF8/C9或CCB/TF8/C9匹配。我尝试过使用\bCB/TF8/C9\b、\\bCB/TF8/C9\\b或\\\bCB/TF8/C9\\\b,但找不到任何匹配的东西。谁能告诉我test需要什么正则表达式才能返回true?
发布于 2021-08-04 07:54:59
当我完全按照你的方式运行你的代码时,它是匹配的。
您正在尝试将"CB/TF8/C9"与以下字符串进行匹配:
This is the description CB/TTF8/C9 and this is more description CB/TF8/C9
CCB/TF8/C9因为your字符串完全包含"CB/TF8/C9",所以它是匹配的。在正则表达式模式的前面或后面放多少\b都无关紧要。
发布于 2021-08-04 13:05:53
在Regexr中,\bCB/TF8/C9\b模式适用于我。也许您应该尝试转义regexValue中的特殊字符。
像这样:string regexValue = @"\bCB/TF8/C9\b";
https://stackoverflow.com/questions/68647217
复制相似问题