Export (0) Print
Expand All

Match.NextMatch Method

Updated: February 2010

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
Assembly:  System (in System.dll)

public Match NextMatch()

Return Value

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

This method is similar to calling Match again and passing (Index+Length) as the new starting position, but it differs from calling Match directly because it handles zero-length match cases in a way that guarantees progress to the end of the string.

NoteNote:

This method does not modify the current instance. Instead, it returns a new Match object that contains information about the next match.

The following example uses the NextMatch method to capture regular expression matches beyond the first match.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

February 2010

Added a Note to the Remarks section.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft