C# Check a word exists in a string

c#, regex

Solution

Yes, a Regex is perfect for the job.

Something like:

string regexPattern = string.Format(@"\b{0}\b", Regex.Escape(yourWord));
if (Regex.IsMatch(yourString, regexPattern)) {
    // word found
}

Problem

Is the best way to do this with Regex? I don't want it picking up partial words for example if I'm search for Gav it shouldn't match Gavin. Any examples would be great as my regular expression skills are non existant. Thanks

Original source