Saturday, May 18, 2013

Visual Basic .Net Regex

Regex handles complex string processing. With the Regex type we use a text-processing language. It handles these cases with less VB.NET code. With Match we search strings. With Replace we change those we find.
These expressions are commonly used to describe patterns. Regular expressions are built from single characters, using union, concatenation, and the Kleene closure, or any-number-of, operator. Aho et al., p. 187

Example

String typeThis simple program demonstrates 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.
And: If the match is successful, we print (with Console.WriteLine) its value, which is "77".
Console.Write
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

NoteIn 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

Example 3

Programming tipNext we see a useful technique when invoking functions from the Regex type in the VB.NET language. 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

No comments:

Post a Comment