Regex.GetGroupNames メソッド

定義

正規表現で使用されるキャプチャ グループ名の配列を返します。

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

戻り値

String[]

グループ名の文字列配列。

次の例では、正規表現グループの名前と一致するテキストを表示する汎用 ShowMatches メソッドを定義します。

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}) は、単純な文を解析し、最初の単語、最後の単語、終了句読点を識別することを目的としています。 次の表は、正規表現パターンの解釈方法を示しています。

Pattern 説明
\b ワード境界から照合を開始します。
(?<FirstWord>\w+) 1 つ以上の単語文字に一致します。 これは名前付き FirstWord グループです。
\s。 0 個または 1 個の空白文字と一致します。
(\w+) 1 つ以上の単語文字に一致します。 これが 2 番目のキャプチャ グループです。
\s 空白文字と一致します。
((\w+)\s)* 1 つ以上の単語文字の後に空白が続く 0 個以上の出現箇所と一致します。 これが最初のキャプチャ グループです。
(?<LastWord>\w+)? 1 つ以上の単語文字の 0 個または 1 回の出現と一致します。 これは名前付き LastWord グループです。
(?<Punctuation>\p{Po}) Unicode カテゴリが句読点 、その他の文字と一致します。 これは名前付き Punctuation グループです。

注釈

グループ名のコレクションには、式のキャプチャ グループに名前を付けるために使用される文字列のセットが含まれています。 キャプチャ グループの名前が明示的に指定されていない場合でも、数値名 ("0"、"1"、"2"、"3" など) が自動的に割り当てられます。 "0" という名前のグループは、正規表現パターンで一致するすべてのテキストを表します。 コレクション内の明示的な名前付きグループの前に番号付きグループがあり、名前付きグループは正規表現パターンで定義されている順序で表示されます。

このメソッドによって返される配列のプロパティを使用 Length して、正規表現内のグループの数を決定できます。

適用対象

こちらもご覧ください