Export (0) Print
Expand All

Regex.Match Method (String, Int32, Int32)

Updated: August 2011

Searches the input string for the first occurrence of a regular expression, beginning at the specified starting position and searching only the specified number of characters.

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

public Match Match(
	string input,
	int beginning,
	int length
)

Parameters

input
Type: System.String

The string to search for a match.

beginning
Type: System.Int32

The zero-based character position in the input string that defines the leftmost position to be searched.

length
Type: System.Int32

The number of characters in the substring to include in the search.

Return Value

Type: System.Text.RegularExpressions.Match
An object that contains information about the match.

ExceptionCondition
ArgumentNullException

input is null.

ArgumentOutOfRangeException

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

-or-

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

-or-

beginning + length – 1 identifies a position that is outside the range of input.

The regular expression pattern for which the Regex.Match(String, Int32, 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.

This method searches the portion of input defined by the beginning and length parameters for the regular expression pattern. beginning always defines the index of the leftmost character to include in the search, and length defines the maximum number of characters to search. Together, they define the range of the search. If the search proceeds from left to right (the default), the regular expression engine searches from the character at index beginning to the character at index beginning + length – 1. If the regular expression engine was instantiated by using the RegexOptions.RightToLeft option so that the search proceeds from right to left, the regular expression engine searches from the character at index beginning + length – 1 to the character at index beginning. This method returns the first match that it finds within this range. You can retrieve subsequent matches by repeatedly calling the returned Match object's NextMatch method.

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.

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

August 2011

Added a discussion of right-to-left searches to the Remarks section.

Content bug fix.

July 2009

Included additional exception information.

Content bug fix.

March 2009

Expanded the Remarks section.

Customer feedback.

October 2008

Expanded the Remarks section.

Customer feedback.

August 2008

Added Remarks.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft