Kris B Kris B - 3 years ago 139
C# Question

Regex.Match whole words


, I want to use a regular expression to match any of these words:

string keywords = "(shoes|shirt|pants)";

I want to find the whole words in the content string. I thought this
would do that:

if (Regex.Match(content, keywords + "\\s+",
RegexOptions.Singleline | RegexOptions.IgnoreCase).Success)

but it returns true for words like
, even though I only want the whole word

How do I match only those literal words?

Answer Source

You should add the word delimiter to your regex:


In code:

Regex.Match(content, @"\b(shoes|shirt|pants)\b");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download