HomeSearch

VB.NET Regex.Match Examples: Regular Expressions

Use Regex from System.Text.RegularExpressions. Match Strings based on patterns.

Regex.

For testing and manipulating text, the Regex class is useful. With Regex, we use a text-processing language. This language easily handles string data.

Functions.

With Match, we search strings. And with Replace, we change those we find. And often RegexOptions are used to change how these functions are evaluated.

Match.

This program uses Regex. Please notice the System.Text.RegularExpressions namespace. The Regex pattern "\d+" matches one or more digit characters together.

Success: We test if the match is successful. If it is, we print (with Console.WriteLine) its value—the string "77."

VB.NET program that uses Regex Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim regex As Regex = New Regex("\d+") Dim match As Match = regex.Match("Dot 77 Perls") If match.Success Then Console.WriteLine(match.Value) End If End Sub End Module Output 77 Pattern details: \d Match digit char. + Match 1 or more chars.

IgnoreCase.

Next, we use different syntax, and an option, for Match. We call the Regex.Match shared Function—no Regex object is needed. We then specify an option, RegexOptions.IgnoreCase.

IgnoreCase: This enum value, a constant, specifies that lower and uppercase letters are equal.

VB.NET program that uses RegexOptions.IgnoreCase Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Match ignoring case of letters. Dim match As Match = Regex.Match("I like that cat", "C.T", RegexOptions.IgnoreCase) If match.Success Then ' Write value. Console.WriteLine(match.Value) End If End Sub End Module Output cat

Groups.

This example uses Match and Groups. We specify the case of letters is unimportant with RegexOptions.IgnoreCase. And finally we test for Success on the Match object received.

Info: When we execute this program, we see the target text was successfully extracted from the input.

Groups index: We use the value 1 to get the first group from the Match. With Regex, indexing starts at 1 not 0 (don't ask why).

VB.NET program that uses Regex.Match Imports System.Text.RegularExpressions Module Module1 Sub Main() ' The input string. Dim value As String = "/content/alternate-1.aspx" ' Invoke the Match method. Dim m As Match = Regex.Match(value, _ "content/([A-Za-z0-9\-]+)\.aspx$", _ RegexOptions.IgnoreCase) ' If successful, write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If End Sub End Module Output alternate-1

Shared.

A Regex object requires time to be created. We can instead share Regex objects, with the shared keyword. A shared Regex object is faster than shared Regex Functions.Shared

Therefore: Storing a Regex as a field in a module or class often results in a speed boost, when Match is called more than once.

Function: The Match function is an instance function on a Regex object. This program has the same result as the previous program.

VB.NET program that uses Match on Regex field Imports System.Text.RegularExpressions Module Module1 ''' <summary> ''' Member field regular expression. ''' </summary> Private _reg As Regex = New Regex("content/([A-Za-z0-9\-]+)\.aspx$", _ RegexOptions.IgnoreCase) Sub Main() ' The input string. Dim value As String = "/content/alternate-1.aspx" ' Invoke the Match method. ' ... Use the regex field. Dim m As Match = _reg.Match(value) ' If successful, write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If End Sub End Module Output alternate-1

Match, NextMatch.

The Match() Function returns the first match only. But we can call NextMatch() on that returned Match object. This is a match that is found in the text, further on.

Tip: NextMatch can be called in a loop. This results in behavior similar to the Matches method (which may be easier to use).

For Each, For
VB.NET program that uses Match, NextMatch Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Get first match. Dim match As Match = Regex.Match("4 and 5", "\d") If match.Success Then Console.WriteLine(match.Value) End If ' Get next match. match = match.NextMatch() If match.Success Then Console.WriteLine(match.Value) End If End Sub End Module Output 4 5

IsMatch.

This returns true if a String matches the regular expression. We get a Boolean that tells us whether a pattern matches. If no other results are needed, IsMatch is useful.

Here: This program introduces the IsValid Boolean function, which computes the result of the Regex.IsMatch function on its parameter.

Note: The regular expression pattern indicates any string of lowercase ASCII letters, uppercase ASCII letters, or digits.

VB.NET program that uses Regex.IsMatch function Imports System.Text.RegularExpressions Module Module1 Function IsValid(ByRef value As String) As Boolean Return Regex.IsMatch(value, "^[a-zA-Z0-9]*$") End Function Sub Main() Console.WriteLine(IsValid("dotnetperls0123")) Console.WriteLine(IsValid("DotNetPerls")) Console.WriteLine(IsValid(":-)")) End Sub End Module Output True True False

Start and ends.

Matching the start and end of a String is commonly-needed. We use the metacharacters "^" and "$" to match the starts and ends of a string.

Note: IsMatch() evaluates these metacharacters in the same way that Match (or Matches) can—the result is different for each function.

VB.NET program that uses IsMatch, tests start and end Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim value As String = "xxyy" ' Match the start with a "^" char. If Regex.IsMatch(value, "^xx") Then Console.WriteLine("ISMATCH START") End If ' Match the end with a "$" char. If Regex.IsMatch(value, "yy$") Then Console.WriteLine("ISMATCH END") End If End Sub End Module Output ISMATCH START ISMATCH END Pattern details: ^ Start of string. xx 2 x chars. yy 2 y chars. $ End of string.

Performance, Compiled Regex.

To optimize Regex performance in VB.NET, we can use the RegexOptions.Compiled enum and store the Regex in a field. Here we test Compiled Regexes.

Version 1: This code uses a Regex field in the Module, and calls IsMatch on the field instance.

Version 2: Uses Regex.IsMatch directly with no stored Regex instance. This code does the same thing as version 1.

VB.NET program that uses RegexOptions.Compiled Imports System.Text.RegularExpressions Module Module1 Dim _regex As Regex = New Regex("X.+0", RegexOptions.Compiled) Sub Version1() ' Use compiled regular expression stored as field. If _regex.IsMatch("X12340") = False Then Throw New Exception End If End Sub Sub Version2() ' Do not use compiled Regex. If Regex.IsMatch("X12340", "X.+0") = False Then Throw New Exception End If End Sub Sub Main() Dim m As Integer = 100000 Dim s1 As Stopwatch = Stopwatch.StartNew ' Version 1: use RegexOptions.Compiled. For i As Integer = 0 To m - 1 Version1() Next s1.Stop() Dim s2 As Stopwatch = Stopwatch.StartNew ' Version 2: do not compile the Regex. For i As Integer = 0 To m - 1 Version2() Next s2.Stop() Dim u As Integer = 1000000 Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) End Sub End Module Output 131.78 ns IsMatch, RegexOptions.Compiled 484.66 ns IsMatch

Matches.

This function is used to locate and return parts of the source String in separate variables. To capture groups, we use parentheses in a Regex pattern.Regex.MatchesRegex.Matches Quote

Some examples.

In these programs, I remove HTML tags. Be careful—this does not work on all HTML. I also count words in English text (this is also not perfect).HTML TagsWord Counts

Replace.

This function takes an optional MatchEvaluator. It will perform both a matching operation and a replacement of the matching parts.Regex.Replace

Split.

Sometimes the String Split function is just not enough for our splitting needs. For these times, try the Regex.Split function.Regex.Split

Files.

See how you can read in the lines of a file, and parse each line, accessing only a part of the text from each line. The StreamReader type and Regex.Match Function are used.Regex File

In some programs,

a Regex function is the easiest way to process text. In others, it adds complexity for little gain. As developers, we must decide when to use Regex.

At its core,

the Regex type exposes a text-processing language, one built upon finite deterministic automata. Tiny programs efficiently manipulate text.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.