VB.NET Regex.Match

Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows

Regex: regular expression

Regex handles complex string processing. With the Regex type we use a text-processing language. It handles these cases with less code. With Match we search strings. And with Replace we change those we find.

Example

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

String face

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

Console
Based on:

.NET 4.5

Program that uses Regex: VB.NET

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

Example 2

ABC: letters

In this example we see an input string, and then invoke the Match method upon it. We specify that the case of letters is unimportant with RegexOptions.IgnoreCase. Finally we test for success on the Match object we received.

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

Program that uses Regex.Match: VB.NET

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

Concept: a discussion topic

Next we see a useful technique when invoking functions from the Regex type. Constructing a regular expression object requires time. Calling a shared Regex function is slower than using a cached Regex object.

Therefore:Storing a Regex as a field in your VB.NET module or class is an appropriate optimization.

And:The Match function can be found as an instance function upon your Regex object. This program has the same result as the previous one.

Program that uses Match on Regex field: VB.NET

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

Functions

Examples

For complex text replacements
and matching,
we apply the Regex type. We specify complex patterns using the text-processing language embedded in the Regex type. We provide examples that can be adapted to any program.

HTML TagsWord Counts

Similar to Match, the Matches and IsMatch functions are used to locate and return parts of the source String in separate variables. To capture groups, you can use parentheses in your Regex pattern.

Regex.MatchesRegex.Matches QuoteRegex.IsMatch

Replace Function. Replace, which takes an optional MatchEvaluator argument, will perform both a matching operation and a replacement of the matching parts based on the arguments you specify.

Regex.ReplaceSplit strings

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

Regex.Split

Summary

The VB.NET programming language

In some programs, a Regex function is the easiest way to process text. In others it adds complexity for little gain. At its core the Regex type exposes a text processing language built upon finite deterministic automata.

VB.NET