Regex.Match whole words

C#.NetRegex

C# Problem Overview


In C#, 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 regex would do that:

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.

How do I match only those literal words?

C# Solutions


Solution 1 - C#

You should add the word delimiter to your regex:

\b(shoes|shirt|pants)\b

In code:

Regex.Match(content, @"\b(shoes|shirt|pants)\b");

Solution 2 - C#

Try

Regex.Match(content, @"\b" + keywords + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase)

\b matches on word boundaries. See here for more details.

Solution 3 - C#

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 \b will 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.

Solution 4 - C#

put a word boundary on it using the \b metasequence.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKris BView Question on Stackoverflow
Solution 1 - C#Philippe LeybaertView Answer on Stackoverflow
Solution 2 - C#Ben LingsView Answer on Stackoverflow
Solution 3 - C#richardtallentView Answer on Stackoverflow
Solution 4 - C#t3rseView Answer on Stackoverflow