Let me elaborate a bit on what Catalin already said.
\w
is the class of characters
"[A-Za-z0-9_]"
. Word boundaries can occurr only right next to these characters. The code below illustrates this quite nicely:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TestSupportService.ServiceReference;
namespace TestSupportService
{
class Program
{
static void Main(string[] args)
{
String example = "I have four child of seven years each, [seven] years ago I had no child, because I was fourteen";
Regex rexWillDo = new Regex(@"\bfour\b|\[\bseven\b\]");
Regex rexWontDo = new Regex(@"\bfour\b|\b\[seven\]\b");
Console.WriteLine("Now you see it!");
MatchCollection matches = rexWillDo.Matches(example);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
Console.WriteLine("\nAnd now you don't!");
matches = rexWontDo.Matches(example);
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
Console.ReadLine();
}
}
}
So by moving the word boundary detectors next to
(real) word characters the expression works. I do admit that I also did not expect that kind of behavior. Regular expressions usually work quite nicely for me, but once in a while MS's implementation of it rears it's ugly head and bites us. :(
Cheers!
—MRB