Regular Expressions in Visual Basic .NET

Our first program demonstrates the use of regular expressions in Visual Basic .NET. Here’s the full source code:

Imports System
Imports System.Text.RegularExpressions

Module RegularExpressions
    Sub Main()
        ' This tests whether a pattern matches a string.
        Dim match As Boolean = Regex.IsMatch("peach", "p([a-z]+)ch")
        Console.WriteLine(match)

        ' For other regex tasks, you'll need to create a Regex object.
        Dim r As New Regex("p([a-z]+)ch")

        ' Many methods are available on these objects. Here's a match test like we saw earlier.
        Console.WriteLine(r.IsMatch("peach"))

        ' This finds the match for the regex.
        Console.WriteLine(r.Match("peach punch").Value)

        ' This also finds the first match but returns the start and end indexes for the match.
        Dim m As Match = r.Match("peach punch")
        Console.WriteLine($"idx: [{m.Index}, {m.Index + m.Length}]")

        ' The Captures property includes information about both the whole-pattern matches 
        ' and the submatches within those matches.
        m = r.Match("peach punch")
        For Each capture As Capture In m.Captures
            Console.Write($"{capture.Value} ")
        Next
        Console.WriteLine()

        ' The All variants of these methods apply to all matches in the input, not just the first.
        ' For example, to find all matches for a regex:
        Dim matches As MatchCollection = r.Matches("peach punch pinch")
        For Each match In matches
            Console.Write($"{match.Value} ")
        Next
        Console.WriteLine()

        ' Providing a non-negative integer as the second argument to these methods will limit the number of matches.
        matches = r.Matches("peach punch pinch", 2)
        For Each match In matches
            Console.Write($"{match.Value} ")
        Next
        Console.WriteLine()

        ' The regex package can also be used to replace subsets of strings with other values.
        Console.WriteLine(r.Replace("a peach", "<fruit>"))

        ' The evaluator variant allows you to transform matched text with a given function.
        Dim result As String = r.Replace("a peach", Function(m) m.Value.ToUpper())
        Console.WriteLine(result)
    End Sub
End Module

To run the program, save the code in a file with a .vb extension (e.g., RegularExpressions.vb) and compile it using the Visual Basic compiler:

$ vbc RegularExpressions.vb
$ mono RegularExpressions.exe
True
True
peach
idx: [0, 5]
peach
peach punch pinch
peach punch
a <fruit>
a PEACH

This example demonstrates various regular expression operations in Visual Basic .NET:

  1. Basic pattern matching
  2. Creating and using Regex objects
  3. Finding matches and their positions
  4. Capturing groups
  5. Finding all matches
  6. Limiting the number of matches
  7. Replacing matched substrings
  8. Using a function to transform matched text

For a complete reference on Visual Basic .NET regular expressions, check the System.Text.RegularExpressions namespace documentation.