Group.Captures 속성

정의

안쪽-왼쪽 우선 순서로 캡처링 그룹을 사용하여 일치시킨 모든 캡처의 컬렉션을 가져옵니다. 정규식을 RightToLeft 옵션으로 수정한 경우에는 안쪽-오른쪽 우선 순서로 가져올 수 있습니다. 컬렉션에는 0이상의 항목이 있을 수 있습니다.

public:
 property System::Text::RegularExpressions::CaptureCollection ^ Captures { System::Text::RegularExpressions::CaptureCollection ^ get(); };
public System.Text.RegularExpressions.CaptureCollection Captures { get; }
member this.Captures : System.Text.RegularExpressions.CaptureCollection
Public ReadOnly Property Captures As CaptureCollection

속성 값

CaptureCollection

그룹을 사용하여 일치시킨 부분 문자열의 컬렉션입니다.

설명

수량자가 캡처링 그룹에 적용되지 않은 경우 속성에서 반환된 Captures 컬렉션에는 개체와 동일한 부분 문자열에 대한 정보를 제공하는 단일 Capture 개체가 Group 포함됩니다. 다음 예에서 확인할 수 있습니다. 문장에서 한 단어를 추출하는 정규식을 \b(\w+)\b정의합니다. 개체는 Group "This"라는 단어를 캡처하고, 개체의 CaptureCollection 단일 개체는 동일한 캡처에 대한 정보를 포함합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\w+)\b";
      string input = "This is one sentence.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr <= match.Groups.Count - 1; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr += 1;                  
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This
//          Group 1:  This
//             Capture 0: This
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\w+)\b"
      Dim input As String = "This is one sentence."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This
'          Group 1:  This
'             Capture 0: This

이 속성의 Captures 실제 유틸리티는 그룹이 단일 정규식에서 여러 부분 문자열을 캡처하도록 캡처링 그룹에 수량자를 적용할 때 발생합니다. 이 경우 개체는 Group 마지막으로 캡처한 부분 문자열에 대한 정보를 포함하는 반면 Captures 속성에는 그룹에서 캡처한 모든 부분 문자열에 대한 정보가 포함됩니다. 다음 예제에서 정규식 \b(\w+\s*)+\. 은 마침표로 끝나는 전체 문장과 일치합니다. 그룹은 (\w+\s*)+ 컬렉션의 개별 단어를 캡처합니다. 컬렉션은 Group 마지막으로 캡처된 부분 문자열에 대한 정보만 포함하므로 문장의 마지막 단어인 "sentence"을 캡처합니다. 그러나 그룹에 의해 캡처된 각 단어는 속성에서 반환된 Captures 컬렉션에서 사용할 수 있습니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is a sentence. This is another sentence.";
      string pattern = @"\b(\w+\s*)+\.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr < match.Groups.Count; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr++; 
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This is a sentence.
//          Group 1:  sentence
//             Capture 0: This
//             Capture 1: is
//             Capture 2: a
//             Capture 3: sentence
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is a sentence. This is another sentence."
      Dim pattern As String = "\b(\w+\s*)+\."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This is a sentence.
'          Group 1:  sentence
'             Capture 0: This
'             Capture 1: is
'             Capture 2: a
'             Capture 3: sentence

적용 대상

추가 정보