Match.NextMatch Match.NextMatch Match.NextMatch Match.NextMatch Method

定義

自最後一個比對結束的位置 (在最後符合字元之後的字元) 開始,傳回具有下一個比對結果的新 Match 物件。Returns a new Match object with the results for the next match, starting at the position at which the last match ended (at the character after the last matched character).

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

傳回

下一個規則運算式相符項目。The next regular expression match.

例外狀況

範例

下列範例會使用NextMatch方法, 來捕捉比第一個相符專案更接近的正則運算式。The following example uses the NextMatch method to capture regular expression matches beyond the first match.

#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) 做為新的開始位置。This method is similar to calling Regex.Match(String, Int32) again and passing (Index+Length) as the new starting position.

注意

這個方法不會修改目前的實例。This method does not modify the current instance. 相反地, 它會傳回Match新的物件, 其中包含下一個相符項的相關資訊。Instead, it returns a new Match object that contains information about the next match.

RegexMatchTimeoutException如果比對作業的超時值已生效, 嘗試尋找下一個相符項時, 嘗試抓取下一個相符項可能會擲回, 而不會有逾時間隔。Attempting to retrieve the next match may throw a RegexMatchTimeoutException if a time-out value for matching operations is in effect and the attempt to find the next match exceeds that time-out interval.

給呼叫者的注意事項

藉由呼叫NextMatch()方法重複進行比對嘗試時, 正則運算式引擎會提供空的符合特殊處理。When a match attempt is repeated by calling the NextMatch() method, the regular expression engine gives empty matches special treatment. 通常會NextMatch()開始搜尋下一個相符項, 完全符合上一個符合項的剩餘位置。Usually, NextMatch() begins the search for the next match exactly where the previous match left off. 不過, 在空的相符項之後NextMatch() , 方法會在嘗試下一個比對之前, 先往一個字元前進。However, after an empty match, the NextMatch() method advances by one character before trying the next match. 這個行為可保證正則運算式引擎會在字串中進行。This behavior guarantees that the regular expression engine will progress through the string. 否則, 由於空的比對不會導致任何正向移動, 因此下一個比對會以與上一個相符的完全相同的位置啟動, 而且會重複符合相同的空字串。Otherwise, because an empty match does not result in any forward movement, the next match would start in exactly the same place as the previous match, and it would match the same empty string repeatedly.

下列範例提供一個實例。The following example provides an illustration. 正則運算式模式a *會在字串 "abaabb" 中搜尋字母 "a" 的零次或多次。The regular expression pattern a* searches for zero or more occurrences of the letter "a" in the string "abaabb". 如範例的輸出所示, 搜尋會尋找六個相符專案。As the output from the example shows, the search finds six matches. 第一個相符嘗試會尋找第一個「a」。The first match attempt finds the first "a". 第二個比對會在第一個比對結束的位置 (在第一個 b 之前) 開始。它會發現 "a" 出現零次, 並傳回空字串。The second match starts exactly where the first match ends, before the first b; it finds zero occurrences of "a" and returns an empty string. 第三個比對的開頭不是第二個比對結束的位置, 因為第二個符合傳回空字串。The third match does not begin exactly where the second match ended, because the second match returned an empty string. 相反地, 它會在第一個 "b" 之後的後面開始一個字元。Instead, it begins one character later, after the first "b". 第三個相符專案會尋找兩個出現的 "a", 並傳回 "aa"。The third match finds two occurrences of "a" and returns "aa". 第四個相符嘗試會從第三個比對結束的位置開始, 在第二個 "b" 之前, 並傳回空字串。The fourth match attempt begins where the third match ended, before the second "b", and returns an empty string. 第五個比對嘗試再次前進一個字元, 使其在第三個 "b" 之前開始, 並傳回空字串。The fifth match attempt again advances one character so that it begins before the third "b" and returns an empty string. 第六個相符項會在最後一個 "b" 之後開始, 並再次傳回空字串。The sixth match begins after the last "b" and returns an empty string again.

[!code-csharpSystem.Text.RegularExpressions.Match.NextMatch#1] [!code-vbSystem.Text.RegularExpressions.Match.NextMatch#1][!code-csharpSystem.Text.RegularExpressions.Match.NextMatch#1] [!code-vbSystem.Text.RegularExpressions.Match.NextMatch#1]

適用於