Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Match::NextMatch Method

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).

Namespace:  System.Text.RegularExpressions
Assemblies:   System.Text.RegularExpressions (in System.Text.RegularExpressions.dll)
  System (in System.dll)

Match^ NextMatch()

Return Value

Type: System.Text.RegularExpressions::Match
The next regular expression match.


A time-out occurred.

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. Instead, it returns a new Match object that contains information about the next match.

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.

Notes to Callers

When a match attempt is repeated by calling the NextMatch method, the regular expression engine gives empty matches special treatment. Usually, NextMatch begins the search for the next match exactly where the previous match left off. 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. 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. The first match attempt finds the first "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. Instead, it begins one character later, after the first "b". The third match finds two occurrences of "a" and returns "aa". The fourth match attempt begins where the third match ended, before the second "b", and returns an empty string. The fifth match attempt again advances one character so that it begins before the third "b" and returns an empty string. The sixth match begins after the last "b" and returns an empty string again.

No code example is currently available or this language may not be supported.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft