System.Text.RegularExpressions.Regex.Matches Method

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

Syntax

public MatchCollection Matches (string input)

Parameters

input
The string to search for a 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) method is similar to the Regex.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:

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

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

The regular expression pattern for which the Regex.Matches(string) method searches is defined by the call to one of the System.Text.RegularExpressions.Regex class constructors. For more information about the elements that can form a regular expression pattern, see Regular Expression Language Elements.

The Regex.Matches(string) 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) 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 the Regex.MatchTimeout property is not Regex.InfiniteMatchTimeout and a matching operation exceeds the 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