Regex.GroupNameFromNumber(Int32) 方法

定義

取得對應指定群組編號的群組名稱。

public:
 System::String ^ GroupNameFromNumber(int i);
public string GroupNameFromNumber (int i);
member this.GroupNameFromNumber : int -> string
Public Function GroupNameFromNumber (i As Integer) As String

參數

i
Int32

群組編號,要轉換至對應群組名稱的。

傳回

字串,含有與指定群組編號相關聯的群組名稱。 如果沒有對應 i 的群組名稱,方法會傳回 Empty

範例

下列範例會定義正則運算式模式,其符合包含美國城市名稱、州名和郵遞區號的位址行。 此範例會 GroupNameFromNumber 使用 方法來擷取擷取群組的名稱。 然後,它會使用這些名稱來擷取對應的擷取群組以取得相符專案。

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)";
      string[] cityLines = {"New York, NY 10003", "Brooklyn, NY 11238", "Detroit, MI 48204", 
                            "San Francisco, CA 94109", "Seattle, WA 98109" };
      Regex rgx = new Regex(pattern);
      List<string> names = new List<string>();
      int ctr = 1;
      bool exitFlag = false;
      // Get group names.
      do {
         string name = rgx.GroupNameFromNumber(ctr);
         if (! String.IsNullOrEmpty(name))
         {
            ctr++;
            names.Add(name);
         }
         else
         {
            exitFlag = true;
         }
      } while (! exitFlag);

      foreach (string cityLine in cityLines)
      {
         Match match = rgx.Match(cityLine);
         if (match.Success)
            Console.WriteLine("Zip code {0} is in {1}, {2}.", 
                               match.Groups[names[3]], 
                               match.Groups[names[1]], 
                               match.Groups[names[2]]);
      } 
   }
}
// The example displays the following output:
//       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.
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
' The example displays the following output:
'       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.

正則運算式模式是由下列運算式所定義:

(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)

下表顯示規則運算式模式的解譯方式。

模式 描述
(?<city>[A-Za-z\s]+) 比對一或多個字母或空白字元。 指派此擷取的群組名稱 city
, 比對逗號 (,) 後面接著空白字元。
(?<state>[A-Za-z]{2}) 比對兩個字母字元。 指派此擷取的群組名稱 state 。 此群組後面應該加上空白字元。
(?<zip>\d{5}(-\d{4})?) 比對五個數字,後面接著零個或一個連字號,後面接著四個數字。 指派此擷取的群組名稱 zip

備註

正則運算式模式可能包含具名或編號的擷取群組,以在模式比對內描述子運算式。 編號群組會以 子運算式 (子運算式) 語法分隔,並根據正則運算式中的順序指派數位。 具名群組是以語法 (分隔? <名字>subexpression) 或 (?'name'subexpression) ,其中 name 是用來識別 subexpression 的名稱。 (如需詳細資訊,請參閱 Grouping Constructs.) 方法 GroupNameFromNumber 會依正則運算式中的序數位置識別具名群組和編號群組。 序數位置零一律代表整個正則運算式。 然後,所有編號的群組都會計算在具名群組之前,不論其在正則運算式模式中的實際位置為何。

如果 i 是具名群組的數目,方法會傳回群組的名稱。 如果 i 是未命名群組的數目,方法會傳回數位的字串表示。 例如,如果 i 為 1,方法會傳回 「1」。 如果 i 不是擷取群組的數目,方法會傳 String.Empty 回 。

如果找到模式比對,則此方法傳回的值接著 Group 可用來擷取代表屬性中擷取之群組的物件 GroupCollection.Item[] 。 屬性 GroupCollection 會傳 Match.Groups 回 物件。

適用於

另請參閱