C# 正则表达式匹配整个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1209049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex.Match whole words
提问by Kris B
In C#
, I want to use a regular expression to match any of these words:
在 中C#
,我想使用正则表达式来匹配以下任何单词:
string keywords = "(shoes|shirt|pants)";
I want to find the whole words in the content string. I thought this regex
would do that:
我想在内容字符串中找到整个单词。我认为这regex
会做到这一点:
if (Regex.Match(content, keywords + "\s+",
RegexOptions.Singleline | RegexOptions.IgnoreCase).Success)
{
//matched
}
but it returns true for words like participants
, even though I only want the whole word pants
.
但是对于像这样的词它返回真participants
,即使我只想要整个词pants
。
How do I match only those literal words?
我如何只匹配那些字面词?
采纳答案by Philippe Leybaert
You should add the word delimiter to your regex:
您应该将单词分隔符添加到您的正则表达式中:
\b(shoes|shirt|pants)\b
In code:
在代码中:
Regex.Match(content, @"\b(shoes|shirt|pants)\b");
回答by t3rse
put a word boundary on it using the \b metasequence.
使用 \b 元序列在其上放置一个单词边界。
回答by richardtallent
You need a zero-width assertion on either side that the characters before or after the word are not part of the word:
您需要在任一侧的零宽度断言单词之前或之后的字符不是单词的一部分:
(?=(\W|^))(shoes|shirt|pants)(?!(\W|$))
As others suggested, I think\bwill work instead of (?=(\W|^))and (?!(\W|$))even when the word is at the beginning or end of the input string, but I'm not sure.
正如其他人所建议的那样,即使单词位于输入字符串的开头或结尾,我认为\b也可以代替(?=(\W|^))和(?!(\W|$)) 工作,但是我我不确定。