Regex::Match Method (String^, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Match^ Match(
	String^ input,
	int startat


Type: System::String^

The string to search for a match.

Type: System::Int32

The zero-based character position at which to start the search.

Return Value

Type: System.Text.RegularExpressions::Match^

An object that contains information about the match.

Exception Condition

input is null.


startat is less than zero or greater than the length of input.


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

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

The regular expression pattern for which the Match(String^, Int32) method searches is defined by the call to one of the Regex class constructors. For more information about the elements that can form a regular expression pattern, see Regular Expression Language - Quick Reference.

You can optionally specify a starting position in the string by using the startat parameter. When the regular expression engine parses from left to right (the default), the match and the scan move rightward, starting at the character specified in startat. When the regular expression engine parses from right to left (when the regular expression pattern is constructed with the RegexOptions::RightToLeft option), the match and scan move in the opposite direction and begin with the character at startat -1. If you do not specify a starting position, the search begins at the default startat position. If the regular expression searches from left to right, the default startat position is at the left end of input; if it searches from right to left, the default startat position is at the right end of input.

If you want to restrict a match so that it begins at a particular character position in the string and the regular expression engine does not scan the remainder of the string for a match, anchor the regular expression with a \G (at the left for a left-to-right pattern, or at the right for a right-to-left pattern). This restricts the match so it must start exactly at startat.

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 found at or after the startat character position 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^, Int32) method.

The RegexMatchTimeoutException exception is thrown if the execution time of the matching operation exceeds the time-out interval specified by the Regex::Regex(String^, RegexOptions, TimeSpan) constructor. If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exeeds any time-out value established for the application domain in which the Regex object is created. If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex::InfiniteMatchTimeout, no exception is thrown.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top