Regex.GetGroupNames 方法

定義

傳回規則運算式的擷取群組名稱的陣列。Returns an array of capturing group names for the regular expression.

public:
 cli::array <System::String ^> ^ GetGroupNames();
public string[] GetGroupNames ();
member this.GetGroupNames : unit -> string[]
Public Function GetGroupNames () As String()

傳回

String[]

群組名稱的字串陣列。A string array of group names.

範例

下列範例會定義一般用途的 ShowMatches 方法,以顯示正則運算式群組的名稱及其相符的文字。The following example defines a general-purpose ShowMatches method that displays the names of regular expression groups and their matched text.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})";
      string input = "The cow jumped over the moon.";
      Regex rgx = new Regex(pattern);
      Match match = rgx.Match(input);
      if (match.Success)
         ShowMatches(rgx, match);
   }

   private static void ShowMatches(Regex r, Match m)
   {
      string[] names = r.GetGroupNames();
      Console.WriteLine("Named Groups:");
      foreach (var name in names) {
         Group grp = m.Groups[name];
         Console.WriteLine("   {0}: '{1}'", name, grp.Value);
      }
   }
}
// The example displays the following output:
//       Named Groups:
//          0: 'The cow jumped over the moon.'
//          1: 'the '
//          2: 'the'
//          FirstWord: 'The'
//          LastWord: 'moon'
//          Punctuation: '.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})"
      Dim input As String = "The cow jumped over the moon."
      Dim rgx As New Regex(pattern)
      Dim match As Match = rgx.Match(input)
      If match.Success Then ShowMatches(rgx, match)
   End Sub
   
   Private Sub ShowMatches(r As Regex, m As Match)
      Dim names() As String = r.GetGroupNames()
      Console.WriteLine("Named Groups:")
      For Each name In names
         Dim grp As Group = m.Groups.Item(name)
         Console.WriteLine("   {0}: '{1}'", name, grp.Value)
      Next
   End Sub
End Module
' The example displays the following output:
'       Named Groups:
'          0: 'The cow jumped over the moon.'
'          1: 'the '
'          2: 'the'
'          FirstWord: 'The'
'          LastWord: 'moon'
'          Punctuation: '.'

在此情況下,正則運算式模式 \b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po}) 是用來剖析簡單的句子,並識別其第一個單字、最後一個字組和結束標點符號。In this case, the regular expression pattern \b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po}) is intended to parse a simple sentence, and to identify its first word, last word, and ending punctuation mark. 下表顯示如何解讀正則運算式模式:The following table shows how the regular expression pattern is interpreted:

模式Pattern 描述Description
\b 開始字緣比對。Begin the match at a word boundary.
(?<FirstWord>\w+) 比對一個或多個文字字元。Match one or more word characters. 這是名為「群組」的 FirstWordThis is the FirstWord named group.
\s?\s? 比對零個或一個空白字元。Match zero or one white-space characters.
(\w+)(\w+) 比對一個或多個文字字元。Match one or more word characters. 這是第二個擷取群組。This is the second capturing group.
\s\s 比對空白字元。Match a white-space character.
((\w +) \s) *((\w+)\s)* 比對出現零次或多次的一或多個文字字元,後面加上空白字元。Match zero or more occurrences of one or more word characters followed by a white space. 這是第一個擷取群組。This is the first capturing group.
(?<LastWord > \w +)嗎?(?<LastWord>\w+)? 比對一或多個文字字元的零或一次出現。Match zero or one occurrence of one or more word characters. 這是名為「群組」的 LastWordThis is the LastWord named group.
(?<標點符號 > \p{Po})(?<Punctuation>\p{Po}) 比對 Unicode 分類為標點符號的字元(其他)。Match a character whose Unicode category is Punctuation, Other. 這是名為「群組」的 PunctuationThis is the Punctuation named group.

備註

組名的集合包含一組用來命名運算式中的「捕獲群組」的字串。The collection of group names contains the set of strings used to name capturing groups in the expression. 即使未明確命名「捕捉群組」,它們也會自動指派數值名稱("0"、"1"、"2"、"3" 等等)。Even if capturing groups are not explicitly named, they are automatically assigned numerical names ("0", "1", "2", "3", and so on). "0" 命名的群組代表正則運算式模式所符合的所有文字。The "0" named group represents all text matched by the regular expression pattern. 編號群組會在集合中明確命名的群組前面,而且命名群組會依照其在正則運算式模式中定義的順序出現。Numbered groups precede explicitly named groups in the collection, and named groups appear in the order in which they are defined in the regular expression pattern.

您可以使用此方法所傳回之陣列上的 Length 屬性,來判斷正則運算式中的群組數目。You can use the Length property on the array returned by this method to determine the number of groups in a regular expression.

適用於

另請參閱