Quantcast
Channel: Regex.Match whole words - Stack Overflow
Browsing index pages (5 articles)
↧

Regex.Match whole words

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:...

View Article


Answer by Philippe Leybaert for Regex.Match whole words

You should add the word delimiter to your regex:\b(shoes|shirt|pants)\bIn code:Regex.Match(content, @"\b(shoes|shirt|pants)\b");

View Article


Answer by t3rse for Regex.Match whole words

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

View Article

Answer by richardtallent for Regex.Match whole words

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...

View Article

Answer by Ben Lings for Regex.Match whole words

TryRegex.Match(content, @"\b"+ keywords + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase)\b matches on word boundaries. See here for more details.

View Article

Browsing index pages (5 articles)


Latest Images