Match.Groups 속성

정의

정규식으로 일치시킨 그룹의 컬렉션을 가져옵니다.

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

속성 값

해당 패턴으로 일치시킨 문자 그룹입니다.

예제

다음 예제에서는 정규식 패턴을 샘플 문자열과 일치시키려고 시도합니다. 이 예제에서는 속성을 사용하여 Groups 콘솔에 표시하기 위해 일치 항목에 의해 검색된 정보를 저장합니다.

#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

설명

정규식 패턴에는 정규식 패턴의 일부를 괄호로 묶어 정의하는 하위 식이 포함될 수 있습니다. 이러한 모든 하위 식은 그룹을 형성합니다. 속성은 Groups 해당 하위 식 일치에 대 한 정보에 대 한 액세스를 제공 합니다. 예를 들어 북미 전화 번호와 일치하는 정규식 패턴 (\d{3})-(\d{3}-\d{4})에는 두 개의 하위 식이 있습니다. 첫 번째는 전화 번호의 처음 세 자리 숫자를 구성하는 지역 번호로 구성됩니다. 이 그룹은 정규식 (\d{3})의 첫 번째 부분인 에 의해 캡처됩니다. 두 번째는 전화 번호의 마지막 7자리를 구성하는 개별 전화 번호로 구성됩니다. 이 그룹은 정규식 (\d{3}-\d{4})의 두 번째 부분인 에 의해 캡처됩니다. 다음 예제와 같이 속성에서 반환 Groups 되는 개체에서 GroupCollection 이러한 두 그룹을 검색할 수 있습니다.

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 개체는 항상 하나 이상의 멤버가 있는 0부터 시작하는 컬렉션 개체입니다. 정규식 엔진이 특정 입력 문자열 Group.Success 에서 일치하는 항목을 찾을 수 없는 경우 컬렉션에 있는 단일 Group 개체의 속성(인덱스 0의 개체)이 로 설정 false 되고 Group 개체의 Value 속성이 로 설정 String.Empty됩니다. 정규식 엔진이 일치 항목을 찾을 수 있는 경우 속성에서 반환된 개체의 GroupCollection 첫 번째 요소(인덱스 0의 Groups 요소)에는 전체 정규식 패턴과 일치하는 문자열이 포함됩니다. 인덱스 1 이상에서 각 후속 요소는 정규식에 캡처링 그룹이 포함된 경우 캡처된 그룹을 나타냅니다. 자세한 내용은 그룹화 구문 문서의 "그룹화 구문 및 정규식 개체" 섹션 을 참조하세요 .

적용 대상