VB.NET Regex.Match

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

Regex: regular expression

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.


String face

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.


Regex: scrambled text in green

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."

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
Lowercase and uppercase words

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.

Program that uses RegexOptions.IgnoreCase: VB.NET

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
ABC: letters

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).

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
Concept

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.

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
Continue

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 Loops
Program that uses Match, NextMatch: VB.NET

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
Example

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
String type

Matches, IsMatch. These functions are 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 QuoteRegex.IsMatch
Replace

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

Regex.Replace
Split

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

Regex.Split
Squares: grey

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,
like gerbils,
efficiently manipulate text.

VB.NET