Matches Method (String)

Regex.Matches Method (String)

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

Searches the specified input string for all occurrences of a regular expression.

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

'Declaration
Public Function Matches ( _
	input As String _
) As MatchCollection

Parameters

input
Type: System.String
The string to search for a match.

Return Value

Type: System.Text.RegularExpressions.MatchCollection
A collection of the Match objects found by the search. If no matches are found, the method returns an empty collection object.

ExceptionCondition
ArgumentNullException

input is Nothing.

The Matches method is similar to the Match method, except that it returns information about all the matches, instead of a single match, found in the input string. It is equivalent to the following code:


Dim match As Match = regex.Match(input)
Do While match.Success
   ' Handle match here...

   match = match.NextMatch()
Loop


The collection includes only successful matches and terminates at the first unsuccessful match.

The regular expression pattern for which the Regex.Matches(String) method searches is defined by the call to one of the Regex class constructors. For more information about the elements that can form a regular expression pattern, see Regular Expression Language Elements in the .NET Framework documentation.

The Matches method uses lazy evaluation to populate the returned MatchCollection object. Accessing such members of this collection as MatchCollection.Count and MatchCollection.CopyTo causes the collection to be populated immediately. To take advantage of lazy evaluation, you should iterate the collection by using a construct such as foreach in C# and ForEachNext in Visual Basic.

Notes to Callers

When a match attempt is repeated by calling the Matches method, the regular expression engine gives empty matches special treatment. Usually, the regular expression engine begins the search for the next match exactly where the previous match left off. However, after an empty match, the regular expression engine 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 resulting MatchCollection object contains six Match objects. 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.


Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "a*"
      Dim input As String = "abaabb"

      For Each m As Match In Regex.Matches(input, pattern)
         outputBlock.Text += String.Format("'{0}' found at index {1}.",  
                           m.Value, m.Index) & vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       'a' found at index 0.
'       '' found at index 1.
'       'aa' found at index 2.
'       '' found at index 4.
'       '' found at index 5.
'       '' found at index 6.


The following example uses the Matches(String) method to identify any words in a sentence that end in "es".


Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "\b\w+es\b"
      Dim rgx As New Regex(pattern)
      Dim sentence As String = "Who writes these notes?"

      For Each match As Match In rgx.Matches(sentence)
         outputBlock.Text += String.Format("Found '{0}' at position {1}", match.Value, match.Index) & vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'writes' at position 4
'       Found 'notes' at position 17


The regular expression pattern \b\w+es\b is defined as shown in the following table.

Pattern

Description

\b

Begin the match at a word boundary.

\w+

Match one or more word characters.

es

Match the literal string "es".

\b

End the match at a word boundary.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft