In C#
,我想使用正则表达式来匹配以下任何单词:
string keywords = "(shoes|shirt|pants)";
我想找到内容字符串中的整个单词。我以为这个regex
会这样做:
if (Regex.Match(content, keywords + "\\s+",
RegexOptions.Singleline | RegexOptions.IgnoreCase).Success)
{
//matched
}
但对于像这样的词它会返回 trueparticipants
,即使我只想要整个单词pants
.
如何只匹配那些字面意思的单词?
您应该将单词分隔符添加到正则表达式中:
\b(shoes|shirt|pants)\b
In code:
Regex.Match(content, @"\b(shoes|shirt|pants)\b");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)