當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。