Regex.GetGroupNumbers 方法

定義

傳回對應陣列中群組名稱的擷取群組編號的陣列。

public:
 cli::array <int> ^ GetGroupNumbers();
public int[] GetGroupNumbers ();
member this.GetGroupNumbers : unit -> int[]
Public Function GetGroupNumbers () As Integer()

傳回

Int32[]

群組編號的整數陣列。

範例

下列範例會定義符合句子的正則運算式 \b((?<word>\w+)\s*)+(?<end>[.?!]) 。 正則運算式包含三個擷取群組:擷取個別單字的未命名群組,以及後面可能會有空白字元;名為 word 的群組會擷取句子中的個別單字,而名為 end 的群組則會擷取結束句子的標點符號。 此範例會 GetGroupNumbers 呼叫 方法來取得所有擷取群組的數目,然後顯示其擷取的字串。 此外, GroupNameFromNumber 方法可用來指出特定編號群組是否對應至具名群組。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b((?<word>\w+)\s*)+(?<end>[.?!])";
      string input = "This is a sentence. This is a second sentence.";
      
      Regex rgx = new Regex(pattern);
      int[] groupNumbers = rgx.GetGroupNumbers();
      Match m = rgx.Match(input);
      if (m.Success) {
         Console.WriteLine("Match: {0}", m.Value);
         foreach (var groupNumber in groupNumbers) {
            string name = rgx.GroupNameFromNumber(groupNumber);
            int number;
            Console.WriteLine("   Group {0}{1}: '{2}'", 
                              groupNumber, 
                              ! string.IsNullOrEmpty(name) & 
                              ! Int32.TryParse(name, out number) ?
                                 " (" + name + ")" : String.Empty, 
                              m.Groups[groupNumber].Value);
         }
      } 
   }
}
// The example displays the following output:
//       Match: This is a sentence.
//          Group 0: 'This is a sentence.'
//          Group 1: 'sentence'
//          Group 2 (word): 'sentence'
//          Group 3 (end): '.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String= "\b((?<word>\w+)\s*)+(?<end>[.?!])"
      Dim input As String = "This is a sentence. This is a second sentence."
      
      Dim rgx As New Regex(pattern)
      Dim groupNumbers() As Integer = rgx.GetGroupNumbers()
      Dim m As Match = rgx.Match(input)
      If m.Success Then
         Console.WriteLine("Match: {0}", m.Value)
         For Each groupNumber In groupNumbers
            Dim name As String = rgx.GroupNameFromNumber(groupNumber)
            Dim number As Integer
            Console.WriteLine("   Group {0}{1}: '{2}'", 
                              groupNumber, 
                              If(Not String.IsNullOrEmpty(name) And 
                              Not Int32.TryParse(name, number),
                                 " (" + name + ")", String.Empty), 
                              m.Groups(groupNumber).Value)
         Next
      End If 
   End Sub
End Module
' The example displays the following output:
'       Match: This is a sentence.
'          Group 0: 'This is a sentence.'
'          Group 1: 'sentence'
'          Group 2 (word): 'sentence'
'          Group 3 (end): '.'

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

模式 描述
\b 開始字緣比對。
(?<word>\w+) 比對一或多個文字字元,並將相符的字串指派給名為 的 word 群組。
\s* 比對零個以上的空白字元。
((?<word>\w+)\s*) 將擷取的 word 群組,後面接著任何擷取的空白字元指派給第一個擷取的群組。
((?<word>\w+)\s*)+ 比對一或多個文字字元的模式,後面接著任何空白字元一或多次。
(?<end>[.?!]) 比對句號、問號或驚嘆號。 將相符的字元指派給 end 擷取群組。

備註

未命名和具名擷取群組都可以依數位存取。 未命名的群組是從左至右從 1 開始編號。 (索引 0 (零) 中的擷取群組代表整個相符專案。) 具名群組接著從左至右編號,從一個大於未命名擷取群組數目的數位開始編號。

依群組的數目來參考群組,而不是透過字串名稱來提供更快的存取。

適用於

另請參閱