当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Regex.GroupNameFromNumber方法代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System.Text.RegularExpressions,代码行数:35,代码来源:Regex.GroupNameFromNumber

输出:

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.


注:本文中的System.Text.RegularExpressions.Regex.GroupNameFromNumber方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。