Match.NextMatch 메서드

정의

마지막 일치 항목이 끝나는 위치(마지막으로 일치한 문자 다음 문자)에서 시작하여 다음 일치 항목에 대한 결과와 함께 새로운 Match 개체를 반환합니다.

public:
 System::Text::RegularExpressions::Match ^ NextMatch();
public System.Text.RegularExpressions.Match NextMatch ();
member this.NextMatch : unit -> System.Text.RegularExpressions.Match
Public Function NextMatch () As Match

반환

Match

다음 정규식 일치 항목입니다.

예외

시간이 초과되었습니다.

예제

다음 예제에서는 메서드를 NextMatch 사용하여 첫 번째 일치 항목 이외의 정규식 일치 항목을 캡처합니다.

#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

설명

이 메서드는 다시 호출 Regex.Match(String, Int32) 하고 (Index+Length)를 새 시작 위치로 전달하는 것과 유사합니다.

참고

이 메서드는 현재 인스턴스를 수정하지 않습니다. 대신 다음 일치 항목에 대한 정보가 포함된 새 Match 개체를 반환합니다.

다음 일치 항목을 검색하려고 하면 일치 작업에 대한 제한 시간 값이 적용되고 다음 일치 항목을 찾으려는 시도가 해당 시간 제한 간격을 초과하는 경우 throw RegexMatchTimeoutException 할 수 있습니다.

호출자 참고

메서드를 호출 NextMatch() 하여 일치 시도가 반복되면 정규식 엔진은 빈 일치 항목에 특별한 처리를 제공합니다. 일반적으로 NextMatch() 이전 경기가 중단된 바로 다음 일치 항목에 대한 검색을 시작합니다. 그러나 빈 일치 후 메서드는 NextMatch() 다음 일치를 시도하기 전에 한 문자씩 진행됩니다. 이 동작은 정규식 엔진이 문자열을 통해 진행되도록 보장합니다. 그렇지 않으면 빈 일치로 인해 정방향 이동이 발생하지 않으므로 다음 일치는 이전 일치와 정확히 동일한 위치에서 시작되며 동일한 빈 문자열과 반복적으로 일치합니다.

다음 예제에서 이에 대해 설명합니다. 정규식 패턴 a* 는 문자열 "abaabb"에서 문자 "a"를 0개 이상 검색합니다. 예제의 출력에서와 같이 검색은 6개의 일치 항목을 찾습니다. 첫 번째 일치 시도는 첫 번째 "a"를 찾습니다. 두 번째 경기는 첫 번째 경기가 종료되는 위치(첫 번째 b 이전)에서 정확히 시작됩니다. "a"가 0번 발생하며 빈 문자열을 반환합니다. 두 번째 일치 항목이 빈 문자열을 반환했기 때문에 세 번째 일치는 두 번째 일치가 종료된 위치로 정확히 시작되지 않습니다. 대신, 첫 번째 "b" 후 나중에 한 문자를 시작합니다. 세 번째 일치 항목은 "a"의 두 항목을 찾아 "aa"를 반환합니다. 네 번째 일치 시도는 두 번째 "b" 이전의 세 번째 일치가 종료된 위치에서 시작되고 빈 문자열을 반환합니다. 다섯 번째 일치 시도는 세 번째 "b" 앞에서 시작하여 빈 문자열을 반환할 수 있도록 한 문자를 다시 진행합니다. 여섯 번째 일치는 마지막 "b" 후에 시작되고 빈 문자열을 다시 반환합니다.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "a*";
      string input = "abaabb";
      
      Match m = Regex.Match(input, pattern);
      while (m.Success) {
         Console.WriteLine("'{0}' found at index {1}.", 
                           m.Value, m.Index);
         m = m.NextMatch();
      }
   }
}
// The example displays the following output:
//       'a' found at index 0.
//       '' found at index 1.
//       'aa' found at index 2.
//       '' found at index 4.
//       '' found at index 5.
//       '' found at index 6.

적용 대상