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)

public static MatchCollection Matches(
	string input,
	string pattern,
	RegexOptions options


Type: System.String
The string to search for a match.
Type: System.String
The regular expression pattern to match.
Type: System.Text.RegularExpressions.RegexOptions
A bitwise combination of the enumeration values.

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.


A regular expression parsing error occurred.


input is null.


pattern is null.


options is not a valid bitwise combination of RegexOptions values.

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 For EachNext 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.

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.

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.

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.




Begin the match at a word boundary.


Match one or more word characters.


Match the literal string "es".


End the match at a word boundary.


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

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




October 2010

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

Information enhancement.

Community Additions