Match Class

Definition

Represents the results from a single regular expression match.

public ref class Match : System::Text::RegularExpressions::Group
public class Match : System.Text.RegularExpressions.Group
[System.Serializable]
public class Match : System.Text.RegularExpressions.Group
type Match = class
    inherit Group
[<System.Serializable>]
type Match = class
    inherit Group
Public Class Match
Inherits Group
Inheritance
Attributes

Remarks

The Match class is immutable and has no public constructor. An instance of the Match class is returned by the Match(String) method and represents the first pattern match in a string. Subsequent matches are represented by Match objects returned by the NextMatch() method. In addition, a MatchCollection object that consists of zero, one, or more Match objects is returned by the Matches(String) method.

If the Matches(String) method fails to match a regular expression pattern in an input string, it returns an empty MatchCollection object. You can then use a foreach construct to iterate over the collection.

If the Match(String) method fails to match the regular expression pattern, it returns a Match object that is equal to Empty. You can use the Success property to determine whether the match was successful.

If a pattern match is successful, the Value property contains the matched substring, the Index property indicates the zero-based starting position of the matched substring in the input string, and the Length property indicates the length of the matched substring in the input string.

Because a single match can involve multiple capturing groups, Match has a Groups property that returns the GroupCollection. The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0], which represents the entire match.

Properties

Name Description
Captures

Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RightToLeft option). The collection may have zero or more items.

(Inherited from Group)
Empty

Gets the empty match. All failed matches return this empty match.

Groups

Gets a collection of groups matched by the regular expression.

Index

Gets the position in the original string where the first character of the captured substring is found.

(Inherited from Capture)
Length

Gets the length of the captured substring.

(Inherited from Capture)
Name

Returns the name of the capturing group represented by the current instance.

(Inherited from Group)
Success

Gets a value indicating whether the match is successful.

(Inherited from Group)
Value

Gets the captured substring from the input string.

(Inherited from Capture)
ValueSpan

Gets the captured span from the input string.

(Inherited from Capture)

Methods

Name Description
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
NextMatch()

Returns a new Match object with the results for the next match, starting at the position at which the last match ended (at the character after the last matched character).

Result(String)

Returns the expansion of the passed replacement pattern. For example, if the replacement pattern is $1$2, Result(String) returns the concatenation of Groups[1].Value and Groups[2].Value.

Synchronized(Match)

Returns a Match instance equivalent to the one supplied that is safe to share between multiple threads.

ToString()

Retrieves the captured substring from the input string by calling the Value property.

(Inherited from Capture)

Applies to

See also