Visual Basic .NET 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

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Recipe
    Private Shared _Regex As Regex = New Regex("(?<!not\s+)likely\b")
    Public Sub Run(ByVal fileName As String)
        Dim line As String
        Dim lineNbr As Integer = 0
        Dim sr As StreamReader = File.OpenText(fileName)
        line = sr.ReadLine
        While Not line Is Nothing
            lineNbr = lineNbr + 1
            If _Regex.IsMatch(line) Then
                Console.WriteLine("Found match '{0}' at line {1}", _
                line, _
                lineNbr)
            End If
            line = sr.ReadLine
        End While
        sr.Close()
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim r As Recipe = New Recipe
        r.Run(args(0))
    End Sub
End Class

[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