Capture.Value 속성

정의

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

public:
 property System::String ^ Value { System::String ^ get(); };
public string Value { get; }
member this.Value : string
Public ReadOnly Property Value As String

속성 값

일치 항목으로 캡처한 부분 문자열입니다.

예제

다음 예제에서는 마침표(".")를 제외하고 문장 부호가 없는 문장과 일치하는 정규식을 정의합니다. 속성은 Match.Value 각 일치 항목에 대해 일치하는 문장으로 구성된 결과 문자열을 표시합니다. 속성은 각 캡처 그룹에 대한 결과 문자열을 표시합니다. 이 문자열은 Group.Value 캡처하는 그룹에서 캡처한 마지막 문자열로 구성됩니다. 속성은 Capture.Value 각 캡처에 대한 결과 문자열을 표시합니다.

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 두 번째 캡처 그룹의 캡처 컬렉션에 있는 개체에는 캡처된 각 단어에 대한 정보가 포함됩니다.

다음 예제에서는 정규식 패턴 를 ^([a-z]+)(\d+)*\.([a-z]+(\d)*)$사용하여 마침표로 구분된 두 부분으로 구성된 제품 번호와 일치합니다. 두 부분 모두 알파벳 문자와 선택적 숫자로 구성됩니다. 첫 번째 입력 문자열이 패턴과 일치하지 않으므로 반환 System.Text.RegularExpressions.Match 된 개체의 Value 속성 값은 입니다 String.Empty. 마찬가지로 정규식 패턴이 캡처하는 그룹과 일치할 수 없는 경우 해당 Group 개체의 Value 속성 값은 입니다 String.Empty.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      String pattern = @"^([a-z]+)(\d+)?\.([a-z]+(\d)*)$";
      String[] values = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" };   
      foreach (var value in values) {
         Match m = Regex.Match(value, pattern, RegexOptions.IgnoreCase);
         if (m.Success) {
            Console.WriteLine("Match: '{0}'", m.Value);
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count);
            for (int gCtr = 0; gCtr < m.Groups.Count; gCtr++) {
               Group group = m.Groups[gCtr];
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 group.Value == "" ? "<empty>" : "'" + group.Value + "'");   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count);
           
               for (int cCtr = 0; cCtr < group.Captures.Count; cCtr++) 
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures[cCtr].Value);
            }
         } 
         else {
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, m.Value == String.Empty ? "<empty>" : m.Value);
         }
      }
   }
}
// The example displays the following output:
//       No match for AC10: Match.Value is <empty>
//       Match: 'Za203.CYM'
//          Number of Capturing Groups: 5
//             Group 0: 'Za203.CYM'
//                Number of Captures: 1
//                   Capture 0: Za203.CYM
//             Group 1: 'Za'
//                Number of Captures: 1
//                   Capture 0: Za
//             Group 2: '203'
//                Number of Captures: 1
//                   Capture 0: 203
//             Group 3: 'CYM'
//                Number of Captures: 1
//                   Capture 0: CYM
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'XYZ.CoA'
//          Number of Capturing Groups: 5
//             Group 0: 'XYZ.CoA'
//                Number of Captures: 1
//                   Capture 0: XYZ.CoA
//             Group 1: 'XYZ'
//                Number of Captures: 1
//                   Capture 0: XYZ
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'CoA'
//                Number of Captures: 1
//                   Capture 0: CoA
//             Group 4: <empty>
//                Number of Captures: 0
//       Match: 'ABC.x170'
//          Number of Capturing Groups: 5
//             Group 0: 'ABC.x170'
//                Number of Captures: 1
//                   Capture 0: ABC.x170
//             Group 1: 'ABC'
//                Number of Captures: 1
//                   Capture 0: ABC
//             Group 2: <empty>
//                Number of Captures: 0
//             Group 3: 'x170'
//                Number of Captures: 1
//                   Capture 0: x170
//             Group 4: '0'
//                Number of Captures: 3
//                   Capture 0: 1
//                   Capture 1: 7
//                   Capture 2: 0
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "^([a-z]+)(\d+)?\.([a-z]+(\d)*)$"
      Dim values() As String = { "AC10", "Za203.CYM", "XYZ.CoA", "ABC.x170" }   
      For Each value In values
         Dim m As Match = Regex.Match(value, pattern, RegexOptions.IgnoreCase)
         If m.Success Then
            Console.WriteLine("Match: '{0}'", m.Value)
            Console.WriteLine("   Number of Capturing Groups: {0}", 
                              m.Groups.Count)
            For gCtr As Integer = 0 To m.Groups.Count - 1
               Dim group As Group = m.Groups(gCtr)
               Console.WriteLine("      Group {0}: {1}", gCtr, 
                                 If(group.Value = "", "<empty>", "'" + group.Value + "'"))   
               Console.WriteLine("         Number of Captures: {0}", 
                                 group.Captures.Count)
           
               For cCtr As Integer = 0 To group.Captures.Count - 1
                  Console.WriteLine("            Capture {0}: {1}", 
                                    cCtr, group.Captures(cCtr).Value)
               Next
            Next
         Else
            Console.WriteLine("No match for {0}: Match.Value is {1}", 
                              value, If(m.Value = String.Empty, "<empty>", m.Value))
         End If
      Next    
   End Sub
End Module
' The example displays the following output:
'       No match for AC10: Match.Value is <empty>
'       Match: 'Za203.CYM'
'          Number of Capturing Groups: 5
'             Group 0: 'Za203.CYM'
'                Number of Captures: 1
'                   Capture 0: Za203.CYM
'             Group 1: 'Za'
'                Number of Captures: 1
'                   Capture 0: Za
'             Group 2: '203'
'                Number of Captures: 1
'                   Capture 0: 203
'             Group 3: 'CYM'
'                Number of Captures: 1
'                   Capture 0: CYM
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'XYZ.CoA'
'          Number of Capturing Groups: 5
'             Group 0: 'XYZ.CoA'
'                Number of Captures: 1
'                   Capture 0: XYZ.CoA
'             Group 1: 'XYZ'
'                Number of Captures: 1
'                   Capture 0: XYZ
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'CoA'
'                Number of Captures: 1
'                   Capture 0: CoA
'             Group 4: <empty>
'                Number of Captures: 0
'       Match: 'ABC.x170'
'          Number of Capturing Groups: 5
'             Group 0: 'ABC.x170'
'                Number of Captures: 1
'                   Capture 0: ABC.x170
'             Group 1: 'ABC'
'                Number of Captures: 1
'                   Capture 0: ABC
'             Group 2: <empty>
'                Number of Captures: 0
'             Group 3: 'x170'
'                Number of Captures: 1
'                   Capture 0: x170
'             Group 4: '0'
'                Number of Captures: 3
'                   Capture 0: 1
'                   Capture 1: 7
'                   Capture 2: 0

정규식 패턴은 다음 표와 같이 정의됩니다.

무늬 설명
^ 문자열의 시작 부분에서 검색을 시작합니다.
([a-z]+) 에서 z까지의 문자를 하나 이상 일치시킬 수 있습니다. 정규식 엔진이 옵션을 전달하므로 RegexOptions.IgnoreCase 이 비교는 대/소문자를 구분하지 않습니다. 이 그룹은 첫 번째 캡처링 그룹입니다.
(\d+)? 하나 이상의 소수 자릿수를 0개 또는 1개 이상 일치시킵니다. 이 그룹은 두 번째 캡처링 그룹입니다.
\. 리터럴 마침표 문자와 일치합니다.
([a-z]+ 에서 z까지의 문자를 하나 이상 일치시킬 수 있습니다. 비교는 대/소문자를 구분합니다.
(\d)* 0번 이상 나오는 10진수를 찾습니다. 일치하는 단일 숫자는 네 번째 캡처 그룹입니다.
([a-z]+(\d)*) 0, 1, 1 이상의 소수 자릿수 뒤에 하나 이상의 알파벳 문자를 1에서 z로 찾습니다. 이 그룹은 네 번째 캡처링 그룹입니다.
$ 문자열의 끝에서 일치 항목을 마무리합니다.

설명

또는 Match.NextMatch 메서드에 대한 호출이 Regex.Match 일치 항목을 찾지 못하면 반환 Match.Value 된 속성의 값은 입니다String.Empty. 정규식 엔진이 캡처 그룹을 일치시킬 수 없는 경우 반환 Group.Value 된 속성의 값은 입니다 String.Empty. 그림에 대한 두 번째 예제를 참조하세요.

적용 대상