Regex.Matches Method (String, Int32)

Updated: October 2010

Searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string.

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

public MatchCollection Matches(
	string input,
	int startat


Type: System.String
The string to search for a match.
Type: System.Int32
The character position in the input string at which to start the search.

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.


input is null.


startat is less than zero or greater than the length of input.

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:

Match match = regex.Match(input, startAt);
while (match.Success)
   // Handle match here...

   match = match.NextMatch();

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

The regular expression pattern for which the Regex.Matches(String, Int32) 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 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 uses the Match(String) method to find the first word in a sentence that ends in "es", and then calls the Matches(String, Int32) method to identify any additional words that end in "es".

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes and uses our paper?";

      // Get the first match.
      Match match = rgx.Match(sentence);
      if (match.Success)
         outputBlock.Text += String.Format("Found first 'es' in '{0}' at position {1}",
                           match.Value, match.Index) + "\n";
         // Get any additional matches.
         foreach (Match m in rgx.Matches(sentence, match.Index + match.Length))
            outputBlock.Text += String.Format("Also found '{0}' at position {1}",
                              m.Value, m.Index) + "\n";
// The example displays the following output:
//       Found first 'es' in 'writes' at position 4
//       Also found 'notes' at position 17
//       Also found 'uses' at position 27

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