本文整理匯總了VB.NET中System.Text.RegularExpressions.Match.Groups屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET Match.Groups屬性的具體用法?VB.NET Match.Groups怎麽用?VB.NET Match.Groups使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Text.RegularExpressions.Match
的用法示例。
在下文中一共展示了Match.Groups屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim text As String = "One car red car blue car"
Dim pattern As String = "(\w+)\s+(car)"
' Instantiate the regular expression object.
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim matchcount as Integer = 0
Do While m.Success
matchCount += 1
Console.WriteLine("Match" & (matchCount))
Dim i As Integer
For i = 1 to 2
Dim g as Group = m.Groups(i)
Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
Dim cc As CaptureCollection = g.Captures
Dim j As Integer
For j = 0 to cc.Count - 1
Dim c As Capture = cc(j)
Console.WriteLine("Capture" & j & "='" & c.ToString() _
& "', Position=" & c.Index)
Next
Next
m = m.NextMatch()
Loop
End Sub
End Module
輸出:
Match1 Group1='One' Capture0='One', Position=0 Group2='car' Capture0='car', Position=4 Match2 Group1='red' Capture0='red', Position=8 Group2='car' Capture0='car', Position=12 Match3 Group1='blue' Capture0='blue', Position=16 Group2='car' Capture0='car', Position=21
示例2: Example
' 導入命名空間
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(\d{3})-(\d{3}-\d{4})"
Dim input As String = "212-555-6666 906-932-1111 415-222-3333 425-888-9999"
Dim matches As MatchCollection = Regex.Matches(input, pattern)
For Each match As Match In matches
Console.WriteLine("Area Code: {0}", match.Groups(1).Value)
Console.WriteLine("Telephone number: {0}", match.Groups(2).Value)
Console.WriteLine()
Next
Console.WriteLine()
End Sub
End Module
輸出:
Area Code: 212 Telephone number: 555-6666 Area Code: 906 Telephone number: 932-1111 Area Code: 415 Telephone number: 222-3333 Area Code: 425 Telephone number: 888-9999