I have the following code. The idea is to detect whole words.
bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false
bool contains = Regex.IsMatch("Hello Hello2", @"\bHello\b"); // yields true
bool contains = Regex.IsMatch("Hello: Hello2", @"\bHello\b"); **// yields true, but should yield false**
Seems that Regex is ignoring the colon. How can I modify the code such that the last line will return false?
\bmeans “word boundary”.:is not part of any word, so the expression is true.Maybe you want an expression like this:
Which means: the string “Hello”, preceded by either the start of the expression or a whitespace, and followed by either the end of the expression or a whitespace.