Match.Groups Match.Groups Match.Groups Match.Groups Property

定義

正規表現に一致したグループのコレクションを取得します。Gets a collection of groups matched by the regular expression.

public:
 virtual property System::Text::RegularExpressions::GroupCollection ^ Groups { System::Text::RegularExpressions::GroupCollection ^ get(); };
public virtual System.Text.RegularExpressions.GroupCollection Groups { get; }
member this.Groups : System.Text.RegularExpressions.GroupCollection
Public Overridable ReadOnly Property Groups As GroupCollection

プロパティ値

パターンに一致した文字グループ。The character groups matched by the pattern.

次の例は、サンプルの文字列に対して正規表現パターンに一致を試みます。The following example attempts to match a regular expression pattern against a sample string. この例では、Groupsコンソールに表示するための一致によって取得される情報を格納するプロパティ。The example uses the Groups property to store information that is retrieved by the match for display to the console.

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
void main()
{
   
   String^ text = "One car red car blue car";
   String^ pat = "(\\w+)\\s+(car)";
   
   // Compile the regular expression.
   Regex^ r = gcnew Regex( pat,RegexOptions::IgnoreCase );
   
   // Match the regular expression pattern against a text string.
   Match^ m = r->Match(text);
   int matchCount = 0;
   while ( m->Success )
   {
      Console::WriteLine( "Match{0}", ++matchCount );
      for ( int i = 1; i <= 2; i++ )
      {
         Group^ g = m->Groups[ i ];
         Console::WriteLine( "Group{0}='{1}'", i, g );
         CaptureCollection^ cc = g->Captures;
         for ( int j = 0; j < cc->Count; j++ )
         {
            Capture^ c = cc[ j ];
            System::Console::WriteLine( "Capture{0}='{1}', Position={2}", j, c, c->Index );
         }
      }
      m = m->NextMatch();
   }
}  
// This example displays the following output:
//       Match1
//       Group1='One'
//       Capture0='One', Position=0
//       Group2='car'
//       Capture0='car', Position=4
//       Match2
//       Group1='red'
//       Capture0='red', Position=8
//       Group2='car'
//       Capture0='car', Position=12
//       Match3
//       Group1='blue'
//       Capture0='blue', Position=16
//       Group2='car'
//       Capture0='car', Position=21
using System;
using System.Text.RegularExpressions;

class Example 
{
   static void Main() 
   {
      string text = "One car red car blue car";
      string pat = @"(\w+)\s+(car)";

      // Instantiate the regular expression object.
      Regex r = new Regex(pat, RegexOptions.IgnoreCase);
      
      // Match the regular expression pattern against a text string.
      Match m = r.Match(text);
      int matchCount = 0;
      while (m.Success) 
      {
         Console.WriteLine("Match"+ (++matchCount));
         for (int i = 1; i <= 2; i++) 
         {
            Group g = m.Groups[i];
            Console.WriteLine("Group"+i+"='" + g + "'");
            CaptureCollection cc = g.Captures;
            for (int j = 0; j < cc.Count; j++) 
            {
               Capture c = cc[j];
               System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
            }
         }
         m = m.NextMatch();
      }
   }
}
// This example displays the following output:
//       Match1
//       Group1='One'
//       Capture0='One', Position=0
//       Group2='car'
//       Capture0='car', Position=4
//       Match2
//       Group1='red'
//       Capture0='red', Position=8
//       Group2='car'
//       Capture0='car', Position=12
//       Match3
//       Group1='blue'
//       Capture0='blue', Position=16
//       Group2='car'
//       Capture0='car', Position=21
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim text As String = "One car red car blue car"
      Dim pattern As String = "(\w+)\s+(car)"

      ' Instantiate the regular expression object.
      Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)

      ' Match the regular expression pattern against a text string.
      Dim m As Match = r.Match(text)
      Dim matchcount as Integer = 0
      Do While m.Success
         matchCount += 1
         Console.WriteLine("Match" & (matchCount))
         Dim i As Integer
         For i = 1 to 2
            Dim g as Group = m.Groups(i)
            Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
            Dim cc As CaptureCollection = g.Captures
            Dim j As Integer 
            For j = 0 to cc.Count - 1
      	      Dim c As Capture = cc(j)
               Console.WriteLine("Capture" & j & "='" & c.ToString() _
                  & "', Position=" & c.Index)
            Next 
         Next 
         m = m.NextMatch()
      Loop
   End Sub
End Module
' This example displays the following output:
'       Match1
'       Group1='One'
'       Capture0='One', Position=0
'       Group2='car'
'       Capture0='car', Position=4
'       Match2
'       Group1='red'
'       Capture0='red', Position=8
'       Group2='car'
'       Capture0='car', Position=12
'       Match3
'       Group1='blue'
'       Capture0='blue', Position=16
'       Group2='car'
'       Capture0='car', Position=21

注釈

正規表現パターンでは、正規表現パターンの一部をかっこで囲んで定義されている部分式を含めることができます。A regular expression pattern can include subexpressions, which are defined by enclosing a portion of the regular expression pattern in parentheses. このようなすべての部分式は、グループを形成します。Every such subexpression forms a group. Groupsプロパティはこれらの部分式一致に関する情報へのアクセスを提供します。The Groups property provides access to information about those subexpression matches. たとえば、正規表現パターン(\d{3})-(\d{3}-\d{4})、2 つの部分式には北アメリカの電話番号に一致します。For example, the regular expression pattern (\d{3})-(\d{3}-\d{4}), which matches North American telephone numbers, has two subexpressions. 1 つ目を表し、最初の 3 つの桁の電話番号の市外局番で構成されます。The first consists of the area code, which composes the first three digits of the telephone number. このグループは、正規表現の最初の部分でキャプチャ(\d{3})します。2 つ目は、個々 の電話番号を表し、電話番号の最後の 7 桁で構成されます。This group is captured by the first portion of the regular expression, (\d{3}).The second consists of the individual telephone number, which composes the last seven digits of the telephone number. このグループは、正規表現の 2 番目の部分によってキャプチャ(\d{3}-\d{4})します。This group is captured by the second portion of the regular expression, (\d{3}-\d{4}). これら 2 つのグループから取得できます、GroupCollectionオブジェクトによって返される、Groupsプロパティは、次の例を示します。These two groups can then be retrieved from the GroupCollection object that is returned by the Groups property, as the following example shows.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\d{3})-(\d{3}-\d{4})";
      string input = "212-555-6666 906-932-1111 415-222-3333 425-888-9999";
      MatchCollection matches = Regex.Matches(input, pattern);
      
      foreach (Match match in matches)
      {
         Console.WriteLine("Area Code:        {0}", match.Groups[1].Value);
         Console.WriteLine("Telephone number: {0}", match.Groups[2].Value);
         Console.WriteLine();
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//       Area Code:        212
//       Telephone number: 555-6666
//       
//       Area Code:        906
//       Telephone number: 932-1111
//       
//       Area Code:        415
//       Telephone number: 222-3333
//       
//       Area Code:        425
//       Telephone number: 888-9999
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(\d{3})-(\d{3}-\d{4})"
      Dim input As String = "212-555-6666 906-932-1111 415-222-3333 425-888-9999"
      Dim matches As MatchCollection = Regex.Matches(input, pattern)
      
      For Each match As Match In matches
         Console.WriteLine("Area Code:        {0}", match.Groups(1).Value)
         Console.WriteLine("Telephone number: {0}", match.Groups(2).Value)
         Console.WriteLine()
      Next
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       Area Code:        212
'       Telephone number: 555-6666
'       
'       Area Code:        906
'       Telephone number: 932-1111
'       
'       Area Code:        415
'       Telephone number: 222-3333
'       
'       Area Code:        425
'       Telephone number: 888-9999

GroupCollectionによって返されるオブジェクト、Match.Groupsプロパティは常に少なくとも 1 つのメンバーを含む 0 から始まるコレクション オブジェクト。The GroupCollection object returned by the Match.Groups property is a zero-based collection object that always has at least one member. 正規表現エンジンでは、特定の入力文字列で、一致した結果が見つからない場合、 Group.Success 、1 つのプロパティGroup(インデックス 0 位置にあるオブジェクト) のコレクション内のオブジェクトに設定されているfalseGroupオブジェクトValueプロパティに設定されてString.Emptyします。If the regular expression engine cannot find any matches in a particular input string, the Group.Success property of the single Group object in the collection (the object at index 0) is set to false and the Group object's Value property is set to String.Empty. 正規表現エンジンが一致の最初の要素を見つけることができるかどうか、GroupCollectionによって返されるオブジェクト (インデックス 0 位置にある要素)、Groupsプロパティには、正規表現パターンに一致する文字列が含まれています。If the regular expression engine can find a match, the first element of the GroupCollection object (the element at index 0) returned by the Groups property contains a string that matches the entire regular expression pattern. 後続の各要素のインデックスから上に、グループを表しますキャプチャされた、正規表現では、キャプチャ グループが含まれる場合。Each subsequent element, from index one upward, represents a captured group, if the regular expression includes capturing groups. 詳細については、の"グループ化を構築し、正規表現オブジェクト」セクションを参照してください、グループ化構成体記事。For more information, see the "Grouping Constructs and Regular Expression Objects" section of the Grouping Constructs article.

適用対象