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 物件,其中包含下一個相符專案的相關資訊。

如果相符作業的逾時值生效,而且嘗試尋找下一個相符專案超過該逾時間隔,則嘗試擷取下一個相符專案可能會擲 RegexMatchTimeoutException 回 。

給呼叫者的注意事項

呼叫 方法重複 NextMatch() 比對嘗試時,正則運算式引擎會提供空的相符專案特殊處理。 通常, NextMatch() 開始搜尋下一個相符專案,完全是上一個相符專案離開的位置。 不過,在空相符專案之後, NextMatch() 方法會在嘗試下一個相符專案之前先前進一個字元。 此行為可確保正則運算式引擎會透過字串進行。 否則,因為空的相符專案不會產生任何向前移動,所以下一個比對會開始的位置與前一個相符專案完全相同,而且會重複比對相同的空字串。

下列範例提供說明。 正則運算式模式 a* 會在字串 「abaabb」 中搜尋零個或多個字母 「a」。 如範例的輸出所示,搜尋會尋找六個相符專案。 第一次比對嘗試會尋找第一個 「a」。 第二個相符專案會從第一個相符專案結束的位置開始,第一個 b;它會尋找零個出現的 「a」,並傳回空字串。 第三個相符專案不會完全開始第二個相符專案結束的位置,因為第二個相符專案傳回空字串。 相反地,它會在第一個 「b」 之後開始一個字元。 第三個相符專案會尋找兩個出現的 「a」,並傳回 「aa」。 第四個比對嘗試會從第三個比對結束處開始,第二個 「b」 之前,並傳回空字串。 第五個比對嘗試會再次前進一個字元,讓它在第三個 「b」 之前開始,並傳回空字串。 第六個比對會在最後一個 「b」 之後開始,並再次傳回空字串。

:::code language=「csharp」 source=「~/snippets/csharp/System.Text.RegularExpressions/Match/NextMatch/nextmatch1.cs」 interactive=「try-dotnet」 id=「Snippet1」:::::code language=「vb」 source=「~/snippets/visualbasic/VS_Snippets_CLR_System/system.text.regularexpressions.match.nextmatch/vb/nextmatch1.vb」 id=「Snippet1」::

適用於