本文整理汇总了VB.NET中System.Text.RegularExpressions.Regex.GroupNameFromNumber方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Regex.GroupNameFromNumber方法的具体用法?VB.NET Regex.GroupNameFromNumber怎么用?VB.NET Regex.GroupNameFromNumber使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.RegularExpressions.Regex
的用法示例。
在下文中一共展示了Regex.GroupNameFromNumber方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim pattern As String = "(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)"
Dim cityLines() As String = {"New York, NY 10003", "Brooklyn, NY 11238", "Detroit, MI 48204", _
"San Francisco, CA 94109", "Seattle, WA 98109" }
Dim rgx As New Regex(pattern)
Dim names As New List(Of String)
Dim ctr As Integer = 1
Dim exitFlag As Boolean = False
' Get group names.
Do
Dim name As String = rgx.GroupNameFromNumber(ctr)
If Not String.IsNullOrEmpty(name) Then
ctr += 1
names.Add(name)
Else
exitFlag = True
End If
Loop While Not exitFlag
For Each cityLine As String In cityLines
Dim match As Match = rgx.Match(cityLine)
If match.Success Then
Console.WriteLine("Zip code {0} is in {1}, {2}.", _
match.Groups.Item(names.Item(3)), _
match.Groups.Item(names.Item(1)), _
match.Groups.Item(names.Item(2)))
End If
Next
End Sub
End Module
输出:
Zip code 10003 is in New York, NY. Zip code 11238 is in Brooklyn, NY. Zip code 48204 is in Detroit, MI. Zip code 94109 is in San Francisco, CA. Zip code 98109 is in Seattle, WA.