Share via


Match.Groups Properti

Definisi

Mendapatkan kumpulan grup yang cocok dengan ekspresi reguler.

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

Nilai Properti

Grup karakter yang cocok dengan pola.

Contoh

Contoh berikut mencoba mencocokkan pola ekspresi reguler dengan string sampel. Contohnya menggunakan Groups properti untuk menyimpan informasi yang diambil oleh kecocokan untuk ditampilkan ke konsol.

#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

Keterangan

Pola ekspresi reguler dapat mencakup subekspresi, yang ditentukan dengan menyertakan sebagian pola ekspresi reguler dalam tanda kurung. Setiap subekspresi tersebut membentuk grup. Properti Groups menyediakan akses ke informasi tentang kecocokan subekspresi tersebut. Misalnya, pola (\d{3})-(\d{3}-\d{4})ekspresi reguler , yang cocok dengan nomor telepon Amerika Utara, memiliki dua subekspresi. Yang pertama terdiri dari kode area, yang menyusun tiga digit pertama nomor telepon. Grup ini ditangkap oleh bagian pertama dari ekspresi reguler, (\d{3}). Yang kedua terdiri dari nomor telepon individu, yang menyusun tujuh digit terakhir dari nomor telepon. Grup ini ditangkap oleh bagian kedua dari ekspresi reguler, (\d{3}-\d{4}). Kedua grup ini kemudian dapat diambil dari GroupCollection objek yang dikembalikan oleh Groups properti , seperti yang ditunjukkan contoh berikut.

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

Objek GroupCollection yang Match.Groups dikembalikan oleh properti adalah objek koleksi berbasis nol yang selalu memiliki setidaknya satu anggota. Jika mesin ekspresi reguler tidak dapat menemukan kecocokan dalam string input tertentu, Group.Success properti objek tunggal Group dalam koleksi (objek pada indeks 0) diatur ke false dan Group properti objek Value diatur ke String.Empty. Jika mesin ekspresi reguler dapat menemukan kecocokan, elemen GroupCollection pertama objek (elemen pada indeks 0) yang dikembalikan oleh Groups properti berisi string yang cocok dengan seluruh pola ekspresi reguler. Setiap elemen berikutnya, dari indeks satu ke atas, mewakili grup yang diambil, jika ekspresi reguler menyertakan penangkapan grup. Untuk informasi selengkapnya, lihat bagian "Konstruksi Pengelompokan dan Objek Ekspresi Reguler" dari artikel Konstruksi Pengelompokan .

Berlaku untuk