Exact/Literal word or pattern match regex

c#, regex, wildcard

Solution

You can use the anchor `\b` for word boundaries:

"\bonly these words\b"

This will match only these words in these sentences:

Here are only these words baby.

Here are "only these words" baby.

Here are only these words, baby.

Here are only these words.

I said: 'only these words'.

Problem

I am trying to match the patterns in a table with user utterance. ``` string userUtterance = "I want identification number for number of customers"; string pattern1 = "identification number"; string pattern2 = "tom"; string pattern3 = "id"; ``` Desired results: ``` bool match1 = regex.Ismatch(userUtterance, pattern1); // should match if(match1 == true) { // replace only the matched pattern in userUtterance }; bool match2 = regex.Ismatch(userUtterance, pattern2); // should not match bool match3 = regex.Ismatch(userUtterance, pattern3); // should not match ``` I would like a little advice on the use of regular expressions matching that syntax to restrict ambiguous matches and exactly match the literal words. Thanks

Original source