NextMatch Method
Collapse the table of content
Expand the table of content

Match.NextMatch Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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 beyond the last matched character).

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

'Declaration
Public Function NextMatch As Match

Return Value

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

This function 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.


Imports System.Text.RegularExpressions
Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim text As String = "One car red car blue car"
      Dim pat As String = "(\w+)\s+(car)"

      ' Instantiate the regular expression object.
      Dim r As Regex = New Regex(pat, 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
         outputBlock.Text += "Match" & (matchcount) & vbCrLf
         Dim i As Integer
         For i = 1 To 2
            Dim g As Group = m.Groups(i)
            outputBlock.Text += "Group" & i & "='" & g.ToString() & "'" & vbCrLf
            Dim cc As CaptureCollection = g.Captures
            Dim j As Integer
            For j = 0 To cc.Count - 1
               Dim c As Capture = cc(j)
               outputBlock.Text += "Capture" & j & "='" & c.ToString() _
                  & "', Position=" & c.Index & vbCrLf
            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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft