Export (0) Print
Expand All

Regex.Match Method (String, Int32)

Searches the input string for the first occurrence of a regular expression with a specified input string starting position.

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

public Match Match(
	string input,
	int startat
)

Parameters

input
Type: System.String
The string to search for a match.
startat
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.

ExceptionCondition
ArgumentNullException

input is null.

ArgumentOutOfRangeException

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

The regular expression pattern for which the Regex.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 Elements in the .NET Framework documentation.

You can optionally specify a starting position in the string 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 want to restrict a match so it does not scan, 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 startpos.

When a Regex is left-to-right (the default), the match and the scan proceed rightward, starting at the character specified in startat. When a Regex is right-to-left (when it is constructed with the RightToLeft option), the match and scan proceed in the opposite direction and begin with the character at startat -1.

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 successful, 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 NextMatch method. You can also retrieve all matches in a single method call by calling the Regex.Matches(String, Int32) method.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft