Capture 클래스

정의

성공한 단일 하위 식 캡처의 결과를 나타냅니다.

public ref class Capture
public class Capture
[System.Serializable]
public class Capture
type Capture = class
[<System.Serializable>]
type Capture = class
Public Class Capture
상속
Capture
파생
특성

예제

다음 예제에서는 마침표(".")를 제외하고 문장 부호가 없는 문장과 일치하는 정규식을 정의합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "Yes. This dog is very friendly.";
      string pattern = @"((\w+)[\s.])+";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("Match: {0}", match.Value);
         for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
               Console.WriteLine("      Capture {0}: {1}", captureCtr, 
                                 group.Captures[captureCtr].Value);
         }                      
      }
   }
}
// The example displays the following output:
//       Match: Yes.
//          Group 0: Yes.
//             Capture 0: Yes.
//          Group 1: Yes.
//             Capture 0: Yes.
//          Group 2: Yes
//             Capture 0: Yes
//       Match: This dog is very friendly.
//          Group 0: This dog is very friendly.
//             Capture 0: This dog is very friendly.
//          Group 1: friendly.
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly.
//          Group 2: friendly
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Yes. This dog is very friendly."
      Dim pattern As String = "((\w+)[\s.])+"
      For Each match As Match In Regex.Matches(input, pattern)
         Console.WriteLine("Match: {0}", match.Value)
         For groupCtr As Integer = 0 To match.Groups.Count - 1
            Dim group As Group = match.Groups(groupCtr)
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            For captureCtr As Integer = 0 To group.Captures.Count - 1
               Console.WriteLine("      Capture {0}: {1}", captureCtr, _
                                 group.Captures(captureCtr).Value)
            Next
         Next                      
      Next
   End Sub
End Module
' The example displays the following output:
'       Match: Yes.
'          Group 0: Yes.
'             Capture 0: Yes.
'          Group 1: Yes.
'             Capture 0: Yes.
'          Group 2: Yes
'             Capture 0: Yes
'       Match: This dog is very friendly.
'          Group 0: This dog is very friendly.
'             Capture 0: This dog is very friendly.
'          Group 1: friendly.
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly.
'          Group 2: friendly
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly

정규식 패턴 ((\w+)[\s.])+ 는 다음 테이블과 같이 정의됩니다. 이 정규식에서는 전체 정규식에 수량자(+)가 적용됩니다.

무늬 설명
(\w+) 하나 이상의 단어 문자를 찾습니다. 이 그룹은 두 번째 캡처링 그룹입니다.
[\s.]) 공백 문자 또는 마침표(".")를 찾습니다.
((\w+)[\s.]) 하나 이상의 단어 문자 뒤에 공백 문자 또는 마침표(".")를 찾습니다. 이 그룹은 첫 번째 캡처링 그룹입니다.
((\w+)[\s.])+ 하나 이상의 단어 문자 또는 문자와 공백 문자 또는 마침표(".")를 찾습니다.

이 예제에서 입력 문자열은 두 문장으로 구성됩니다. 출력에서 알 수 있듯이 첫 번째 문장은 하나의 단어로만 구성되므로 CaptureCollection 개체에는 개체와 동일한 캡처를 나타내는 단일 Capture 개체가 Group 있습니다. 두 번째 문장은 여러 단어로 구성되므로 개체에는 Group 마지막으로 일치하는 하위 식에 대한 정보만 포함됩니다. 첫 번째 캡처를 나타내는 그룹 1에는 닫는 기간이 있는 문장의 마지막 단어가 포함됩니다. 두 번째 캡처를 나타내는 그룹 2에는 문장의 마지막 단어가 포함됩니다. 그러나 Capture 그룹의 CaptureCollection 개체에 있는 개체는 각 하위 식 일치를 캡처합니다. 첫 번째 캡처 그룹의 캡처 컬렉션에 있는 개체에는 Capture 캡처된 각 단어와 공백 문자 또는 마침표에 대한 정보가 포함됩니다. Capture 두 번째 캡처 그룹의 캡처 컬렉션에 있는 개체에는 캡처된 각 단어에 대한 정보가 포함됩니다.

설명

Capture 개체는 변경할 수 없으며 공용 생성자가 없습니다. 인스턴스는 개체를 CaptureCollection 통해 반환되며, 개체는 해당 개체 및 Group.Captures 속성에 Match.Captures 의해 반환됩니다. 그러나 이 속성은 Match.Captures 개체와 동일한 일치 Match 에 대한 정보를 제공합니다.

캡처링 그룹에 수량자를 적용하지 않으면 이 속성은 Group.Captures 개체와 동일한 캡처에 대한 정보를 제공하는 단일 Capture 개체를 Group 반환 CaptureCollection 합니다. 캡처링 그룹에 Group.Index수량자를 적용하는 경우 , Group.LengthGroup.Value 속성은 마지막으로 캡처된 그룹에 대한 정보만 제공하는 반면 Capture , 개체는 CaptureCollection 모든 하위 식 캡처에 대한 정보를 제공합니다. 예제에서는 그림을 제공합니다.

속성

Index

원래 문자열에서 캡처된 부분 문자열의 첫째 문자를 찾은 위치입니다.

Length

캡처된 부분 문자열의 길이를 가져옵니다.

Value

입력 문자열에서 캡처된 부분 문자열을 가져옵니다.

ValueSpan

입력 문자열에서 캡처된 범위를 가져옵니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

Value 속성을 호출하여 입력 문자열로부터 캡처된 하위 문자열을 검색합니다.

적용 대상

추가 정보