本文整理汇总了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