Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0652 Word Boundaries

The word boundary assertion \b matches where word characters (\w) adjoin either: Nonword characters (\W) The beginning/end of the string (^ and $) \b is often used to match whole words. For example: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { foreach (Match m in Regex.Matches ("This is a test", @"\b\w+\b")) { Console.WriteLine (m); } } } The output: This is a test The following statements highlight the effect of a word boundary: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { int one = Regex.Matches ("This is a test", @"\bis\b").Count; Console.WriteLine(one); int two = Regex.Matches ("This isn't a test", @"is").Count; Console.WriteLine(two); } } The output: 1 2 The next query uses positive lookahead to return words followed by "(is)": using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string text = "This (is) a test"; Console.Write (Regex.Match (text, @"\b\w+\b\s(?=\(is\))")); } } The output: This