Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Regex::Matches Method (String, String)

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

Namespace:  System.Text.RegularExpressions
Assemblies:   System.Text.RegularExpressions (in System.Text.RegularExpressions.dll)
  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 or pattern is nullptr.

The Matches(String, String) method is similar to the 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:

No code example is currently available or this language may not be supported.

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 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 - 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 For EachNext in Visual Basic.

Because of its lazy evaluation, calling the Matches(String, 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 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.

Notes to Callers

This method times out after an interval that is equal to the default time-out value of the application domain in which it is called. If a time-out value has not been defined for the application domain, the value Regex::InfiniteMatchTimeout, which prevents the method from timing out, is used. The recommended static method for retrieving multiple pattern matches is Regex::Matches(String, String, RegexOptions, TimeSpan), which lets you specify the time-out interval.

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

No code example is currently available or this language may not be supported.

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.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Show:
© 2015 Microsoft