Export (0) Print
Expand All

Regex.Matches Method (String, String)

Updated: September 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 function Matches(
	input : String, 
	pattern : String
) : MatchCollection

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 has occurred.

ArgumentNullException

input is a null reference (Nothing in Visual Basic).

-or-

pattern is a null reference (Nothing in Visual Basic).

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 Matches method.

The pattern parameter consists of various 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 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.

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2010

Expanded the Return Value and Remarks sections.

Customer feedback.

February 2010

Added an example.

Customer feedback.

July 2009

Included additional exception information.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft