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>[.?!])文に一致する正規表現 を定義します。 正規表現には、3 つのキャプチャ グループが含まれています。個々の単語とその後に続く可能性のあるスペース文字をキャプチャする名前のないグループです。文内の個々の単語をキャプチャする という名前 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+) 1 つ以上の単語文字と一致し、一致した文字列を という名前 wordのグループに割り当てます。
\s* 0 個以上の空白文字と一致します。
((?<word>\w+)\s*) キャプチャされたグループの word 後にキャプチャされた空白文字を、最初にキャプチャしたグループに割り当てます。
((?<word>\w+)\s*)+ 1 つ以上の単語文字のパターンと一致し、その後に空白文字が 1 回以上続きます。
(?<end>[.?!]) ピリオド、疑問符、または感嘆符に一致します。 一致した文字をキャプチャ グループに end 割り当てます。

注釈

名前のないキャプチャ グループと名前付きキャプチャ グループの両方に、番号でアクセスできます。 名前のないグループには、1 から始まる左から右に番号が付けられます。 (インデックス 0 (ゼロ) のキャプチャ グループは、一致を全体として表します)。 名前付きグループは、名前のないキャプチャ グループの数より 1 大きい数値で始まる左から右に番号が付けられます。

文字列名ではなく番号でグループを参照すると、より高速なアクセスが可能になります。

適用対象

こちらもご覧ください