Liviu Mandras
Liviu Mandras

Reputation: 6627

Regex and the colon (:)

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?

Upvotes: 6

Views: 10429

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

To match a whole word not directly followed with a colon, use

\bHello\b(?!:)
\bHello(?![:\w])

See the regex demo. Details:

  • \b - a word boundary
  • Hello - a word
  • (?![:\w]) - a negative lookahead that fails the match if there is : or a word char immediately to the right of the current location.

Se the C# code demo:

bool contains = Regex.IsMatch("Hello: Hello2", @"\bHello\b");
Console.WriteLine(contains); // => False
Console.WriteLine(Regex.IsMatch("Hello: Hello2", @"\bHello(?![:\w])"));
// => False

Upvotes: 0

Sebastian Schmidt
Sebastian Schmidt

Reputation: 1078

The Regex isn't ignoring the colon. The position before the colon is where \b matches, because \b matches word-boundaries. That means the position between a word-character and a non-word-chracter.

If you want Whitespace to follow after your word 'Hello', than use "\bHello\s".

Upvotes: 3

Fábio Batista
Fábio Batista

Reputation: 25280

\b means "word boundary". : is not part of any word, so the expression is true.

Maybe you want an expression like this:

(^|\s)Hello(\s|$)

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.

Upvotes: 12

Related Questions