Visual Basic .NET Search for Lines Ending with a Word

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

This recipe finds full words at the end of a line. For the purposes of this example, that word is finale.

[edit] code

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Recipe
    Private Shared _Regex As Regex = New Regex("\bfinale$")
        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

The key to this expression is the use of character classes. Two of them are used, one to define the end of the line and one to define a word boundary, so you get only whole words when using the expression. You can break the regular expression down like this:

Regular Expression Description
\b is a word boundary, such as a space, tab, and so on, followed by . . .
finale f, i, n, a, l, followed by e and lastly . . .
$ the end of the line.

The line anchor $ is like the line anchor ^ because both of them are supported in nearly every flavor of regular expression. The “Syntax Overview” section at the beginning of this book highlights the different character classes supported in the various sections of regular expressions.

[edit] Variations

You may be interested in matching lines where the word is the last word on the line, which means spaces may appear between the end of the word and the end of the line. To modify the expression to match lines with possible spaces, use the character class that matches spaces with the * qualifier, which matches none or many. The regular expression variation looks like this: \bfinale\s*$.

Personal tools