Word Boundaries

The word boundary assertion \b matches where word characters (\w) adjoin either:

\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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.