Regex.Matches Method (String, String, RegexOptions)

Updated: October 2010

Searches the specified input string for all occurrences of the regular expression supplied in a pattern parameter with matching options supplied in an options parameter.

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

Syntax

'Declaration
Public Shared Function Matches ( _
    input As String, _
    pattern As String, _
    options As RegexOptions _
) As MatchCollection
public static MatchCollection Matches(
    string input,
    string pattern,
    RegexOptions options
)

Parameters

  • pattern
    Type: System.String
    The regular expression pattern to 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.

Exceptions

Exception Condition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

-or-

pattern is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

options is not a valid bitwise combination of RegexOptions values.

Remarks

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:

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

The static Matches methods are equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance method Matches.

The pattern parameter consists of various regular expression language elements that symbolically describe the string to match. For more information about regular expressions, 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 ForEach…Next 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.
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string pattern = "a*";
      string input = "abaabb";

      foreach (Match m in Regex.Matches(input, pattern))
         outputBlock.Text += String.Format("'{0}' found at index {1}.",
                           m.Value, m.Index) + "\n";
   }
}
// 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.

Examples

The following example calls the Matches(String, String) method to identify any word in a sentence that ends in "es", and then calls the Matches(String, String, RegexOptions) to perform a case-insensitive comparison of the pattern with the input string. As the output shows, the two methods return different results.

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 sentence As String = "NOTES: Any notes or comments are optional."

      ' Call Matches method without specifying any options.
      For Each match As Match In Regex.Matches(sentence, pattern)
         outputBlock.Text += String.Format("Found '{0}' at position {1}", match.Value, match.Index) & vbCrLf
      Next
      outputBlock.Text &= vbCrLf

      ' Call Matches method for case-insensitive matching.
      For Each match As Match In Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase)
         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 'notes' at position 11
'       
'       Found 'NOTES' at position 0
'       Found 'notes' at position 11
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";

      // Call Matches method without specifying any options.
      foreach (Match match in Regex.Matches(sentence, pattern))
         outputBlock.Text += String.Format("Found '{0}' at position {1}",
                           match.Value, match.Index) + "\n";
      outputBlock.Text += "\n";

      // Call Matches method for case-insensitive matching.
      foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
         outputBlock.Text += String.Format("Found '{0}' at position {1}",
                           match.Value, match.Index) + "\n";
   }
}
// The example displays the following output:
//       Found 'notes' at position 11
//       
//       Found 'NOTES' at position 0
//       Found 'notes' at position 11

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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

October 2010

Expanded the Remarks section and added the Notes to Callers section.

Information enhancement.