Matches Method (String)

Regex.Matches Method (String)


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

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

public MatchCollection Matches(
	string 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.

Exception Condition

input is null.

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

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

      match = match.NextMatch();

The collection includes only matches and terminates at the first non-match.

The regular expression pattern for which the 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 - Quick Reference.

The Matches method uses lazy evaluation to populate the returned MatchCollection object. Accessing members of this collection such 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.

Because of its lazy evaluation, calling the Matches(String) method does not throw a RegexMatchTimeoutException exception. However, the exception is thrown when an operation is performed on the MatchCollection object returned by this method, if the MatchTimeout property is not Regex.InfiniteMatchTimeout and a matching operation exceeds the time-out interval.

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

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes?";

      foreach (Match match in rgx.Matches(sentence))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
// 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.




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.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2015 Microsoft