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