Export (0) Print
Expand All

Regex::Match Method (String, String)

Searches the specified input string for the first occurrence of the specified regular expression.

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

public:
static Match^ Match(
	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::Match
An object that contains information about the match.

ExceptionCondition
ArgumentException

A regular expression parsing error occurred.

ArgumentNullException

input or pattern is nullptr.

RegexMatchTimeoutException

A time-out occurred. For more information about time-outs, see the Remarks section.

The Match(String, String) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language - Quick Reference.

The static Match(String, String) method is equivalent to constructing a Regex object with the specified regular expression pattern and calling the instance Match(String) method. In this case, the regular expression engine caches the regular expression pattern.

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.

You can determine whether the regular expression pattern has been found in the input string by checking the value of the returned Match object's Success property. If a match is found, the returned Match object's Value property contains the substring from input that matches the regular expression pattern. If no match is found, its value is String::Empty.

This method returns the first substring in input that matches the regular expression pattern. You can retrieve subsequent matches by repeatedly calling the returned Match object's Match::NextMatch method. You can also retrieve all matches in a single method call by calling the Regex::Matches(String, String) method.

The RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified for the application domain in which the method is called. If no time-out is defined in the application domain's properties, or if the time-out value is Regex::InfiniteMatchTimeout, no exception is thrown.

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 a pattern match is Regex::Match(String, String), which lets you set the time-out interval.

The following example calls the Match(String, String) method to find the first word that contains at least one z character, and then calls the Match::NextMatch method to find any additional matches.

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

The regular expression pattern \b\w*z+\w*\b is interpreted as shown in the following table.

Pattern

Description

\b

Begin the match at a word boundary.

\w*

Match zero, one, or more word characters.

z+

Match one or more occurrences of the z character.

\w*

Match zero, one, or more word characters.

\b

End the match at a word boundary.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

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

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft