Regex.Match whole words

Solution 1:

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:

Try

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

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

Solution 3:

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.