System.Text.RegularExpressions.Regex.Matches Method

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

Syntax

public MatchCollection Matches (string input, int startat)

Parameters

input
The string to search for a match.
startat
The character position in the input string at which to start the search.

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, int) method is similar to the Regex.Match(string, int) 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#6

The regular expression pattern for which the Regex.Matches(string, int) 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, 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, int) 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