System.Text.RegularExpressions.Match Class

Represents the results from a single regular expression match.

See Also: Match Members


public class Match : Group


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

If the Regex.Matches(string) method fails to match a regular expression pattern in an input string, it returns an empty System.Text.RegularExpressions.MatchCollection object. You can then use a foreach construct in C# or a For Each construct in Visual Basic to iterate the collection.

If the Regex.Match(string) method fails to match the regular expression pattern, it returns a System.Text.RegularExpressions.Match object that is equal to Match.Empty. You can use the Group.Success property to determine whether the match was successful. The following example provides an illustration.

code reference: System.Text.RegularExpressions.Match.Class#1

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

Because a single match can involve multiple capturing groups, System.Text.RegularExpressions.Match has a Match.Groups property that returns the System.Text.RegularExpressions.GroupCollection. The System.Text.RegularExpressions.GroupCollection has accessors that return each group. The System.Text.RegularExpressions.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.


Namespace: System.Text.RegularExpressions
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0,,