System.Text.RegularExpressions.Regex.Matches Method

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

Syntax

public static MatchCollection Matches (string input, string pattern)

Parameters

input
The string to search for a match.
pattern
The regular expression pattern to match.

Returns

A collection of the System.Text.RegularExpressions.Match objects found by the search. If no matches are found, the method returns an empty collection object.

Remarks

The Regex.Matches(string, string) method is similar to the Regex.Match(string, 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:

code reference: System.Text.RegularExpressions.Regex.Matches#7

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

The pattern parameter consists of regular expression language elements that symbolically describe the string to match. For more information about regular expressions, see .NET Framework Regular Expressions and Regular Expression Language Elements.

The Regex.Matches(string, int) method uses lazy evaluation to populate the returned System.Text.RegularExpressions.MatchCollection object. Accessing members of this collection such as MatchCollection.Count and MatchCollection.CopyTo(Array, int) 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 Each…Next in Visual Basic.

Because of its lazy evaluation, calling the Regex.Matches(string, string) method does not throw a System.Text.RegularExpressions.RegexMatchTimeoutException exception. However, the exception is thrown when an operation is performed on the System.Text.RegularExpressions.MatchCollection object returned by this method, if a time-out interval is defined by the "REGEX_DEFAULT_MATCH_TIMEOUT" property of the current application domain and a matching operation exceeds this time-out interval.

Requirements

Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0