Regex.GroupNameFromNumber(Int32) 方法

定義

取得對應指定群組編號的群組名稱。Gets the group name that corresponds to the specified group number.

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

群組編號,要轉換至對應群組名稱的。The group number to convert to the corresponding group name.

傳回

字串,含有與指定群組編號相關聯的群組名稱。A string that contains the group name associated with the specified group number. 如果沒有對應 i 的群組名稱,方法會傳回 EmptyIf there is no group name that corresponds to i, the method returns Empty.

範例

下列範例會定義正則運算式模式,以符合包含美國城市名稱、州名稱和郵遞區號的位址行。The following example defines a regular expression pattern that matches an address line containing a U.S. city name, state name, and zip code. 此範例會使用 GroupNameFromNumber 方法來抓取「捕捉群組」的名稱。The example uses the GroupNameFromNumber method to retrieve the names of capturing groups. 然後,它會使用這些名稱來抓取對應的相符已捕捉群組。It then uses these names to retrieve the corresponding captured groups for matches.

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.

正則運算式模式是由下列運算式所定義:The regular expression pattern is defined by the following expression:

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

下表顯示規則運算式模式的解譯方式。The following table shows how the regular expression pattern is interpreted.

模式Pattern 描述Description
(?<city>[A-Za-z\s]+) 符合一或多個字母或空白字元。Match one or more alphabetic or white-space character. 將此已捕獲的群組指派 city的名稱。Assign this captured group the name city.
, 比對後面接著空白字元的逗號(,)。Match a comma (,) followed by a white-space character.
(?<state>[A-Za-z]{2}) 符合兩個字母字元。Match two alphabetic characters. 將此已捕獲的群組指派 state的名稱。Assign this captured group the name state. 此群組後面應該加上空白字元。This group should be followed by a white-space character.
(?<zip>\d{5}(-\d{4})?) 比對五個數字,後面接著零個或一個出現的連字號,後面接著四個數字。Match five numeric digits followed by either zero or one occurrence of a hyphen followed by four digits. 將此已捕獲的群組指派 zip的名稱。Assign this captured group the name zip.

備註

正則運算式模式可以包含已命名或編號的「捕捉群組」,其中描述模式比對中的子運算式。A regular expression pattern may contain either named or numbered capturing groups, which delineate subexpressions within a pattern match. 編號群組是以語法(子運算式)分隔,並根據其在正則運算式中的順序指派數位。Numbered groups are delimited by the syntax (subexpression) and are assigned numbers based on their order in the regular expression. 命名群組是以語法(?<名稱>子運算式)或(?)分隔名稱'子運算式),其中name是用來識別子運算式的名稱。Named groups are delimited by the syntax (?<name>subexpression) or (?'name'subexpression), where name is the name by which the subexpression will be identified. (如需詳細資訊,請參閱群組結構)。GroupNameFromNumber 方法會依其在正則運算式中的序數位置,識別命名群組和編號群組。(For more information, see Grouping Constructs.) The GroupNameFromNumber method identifies both named groups and numbered groups by their ordinal positions in the regular expression. 序數位置零一律代表整個正則運算式。Ordinal position zero always represents the entire regular expression. 所有編號群組會接著在命名群組之前計算,而不論其在正則運算式模式中的實際位置為何。All numbered groups are then counted before named groups, regardless of their actual position in the regular expression pattern.

如果 i 是命名群組的數目,則方法會傳回群組的名稱。If i is the number of a named group, the method returns the name of the group. 如果 i 是未命名群組的數目,則方法會傳回數位的字串表示。If i is the number of an unnamed group, the method returns the string representation of the number. 例如,如果 i 是1,則方法會傳回 "1"。For example, if i is 1, the method returns "1". 如果 i 不是「捕捉」群組的數目,則此方法會傳回 String.EmptyIf i is not the number of a capturing group, the method returns String.Empty.

如果找到模式比對,則可以使用這個方法傳回的值,從 GroupCollection.Item[String] 屬性中取出代表已捕獲群組的 Group 物件。If a pattern match is found, the value returned by this method can then be used to retrieve the Group object that represents the captured group from the GroupCollection.Item[String] property. Match.Groups 屬性會傳回 GroupCollection 物件。The GroupCollection object is returned by the Match.Groups property.

適用於

另請參閱