Share via


Match Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Represents the results from a single regular expression match.

Inheritance Hierarchy

System. . :: . .Object
  System.Text.RegularExpressions. . :: . .Capture
    System.Text.RegularExpressions. . :: . .Group
      System.Text.RegularExpressions..::..Match

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

Syntax

'Declaration
<SerializableAttribute> _
Public Class Match _
    Inherits Group
[SerializableAttribute]
public class Match : Group
[SerializableAttribute]
public ref class Match : public Group
[<SerializableAttribute>]
type Match =  
    class
        inherit Group
    end
public class Match extends Group

The Match type exposes the following members.

Properties

  Name Description
Public property 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 RegexOptions.RightToLeft option). The collection may have zero or more items. (Inherited from Group.)
Public propertyStatic member Empty Gets the empty group. All failed matches return this empty match.
Public property Groups Gets a collection of groups matched by the regular expression.
Public property Index The position in the original string where the first character of the captured substring is found. (Inherited from Capture.)
Public property Length Gets the length of the captured substring. (Inherited from Capture.)
Public property Success Gets a value indicating whether the match is successful. (Inherited from Group.)
Public property Value Gets the captured substring from the input string. (Inherited from Capture.)

Top

Methods

  Name Description
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method 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).
Public method Result Returns the expansion of the specified replacement pattern.
Public methodStatic member Synchronized Returns a Match instance equivalent to the one supplied that is suitable to share between multiple threads.
Public method ToString Retrieves the captured substring from the input string by calling the Value property. (Inherited from Capture.)

Top

Remarks

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

If the Regex..::..Match method fails to match the regular expression pattern, it returns a Match object that is equal to Match..::..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 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 GroupCollection has accessors that return each group. The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0] (Match.Groups(0) in Visual Basic), which represents the entire match.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Text.RegularExpressions Namespace