C Sharp Find Words Not Preceded by Other Words

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

This recipe finds a word likely in text but will skip over it if the word before it is not. It provides an example of how to use a negative look-behind.

[edit] code

using System;
using System.IO;
using System.Text.RegularExpressions;
public class Recipe
{
    private static Regex _Regex = new Regex( @"(?<!not\s+)likely\b" );
    public void Run(string fileName)
    {
        String line;
        int lineNbr = 0;
        using (StreamReader sr = new StreamReader(fileName))
        {
            while(null != (line = sr.ReadLine()))
            {
                lineNbr++;
                if (_Regex.IsMatch(line))
                {
                    Console.WriteLine("Found match '{0}' at line {1}",
                    line,
                    lineNbr);
                }
            }
        }
    }
    public static void Main( string[] args )
    {
        Recipe r = new Recipe();
        r.Run(args[0]);
    }
}

[edit] How It Works

This recipe uses a negative look-behind as it’s searching through the string to make sure what you’re looking for, likely\b, isn’t preceded by not\s+. It breaks down like this:

Regular Expression Description
(?<! the negative look-behind that matches . . .
not the characters n, o, and t, followed by . . .
\s a space . . .
+ found one or more times . . .
) the end of the look-behind . . .
likely the letters l, i, k, e, l, and y, followed by . . .
\b a word boundary character.
Personal tools