Export (0) Print
Expand All

Regex.Matches Method (String, String)

Updated: October 2010

Searches the specified input string for all occurrences of the regular expression specified in the pattern parameter.

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

public static MatchCollection Matches(
	string input,
	string pattern
)

Parameters

input
Type: System.String
The string to search for a match.
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.

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input is null.

-or-

pattern is null.

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 uses the Matches(String, String) method to identify any word in a sentence that ends 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";
      string sentence = "Who writes these notes?";

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

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

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

Date

History

Reason

October 2010

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

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft