Regular Expression Result Capture Class Properties Methods Remarks Applies to See also CaptureCollection Class Properties Methods Extension Methods Remarks Applies to See also Group Class Properties Methods Remarks Applies to GroupCollection Class Properties Methods Extension Methods Remarks Applies to See also Examples Source/Reference
Regular Expression Result
The results of the regular expression engine of .NET framework can be accessed by Capture Class, CaptureCollection Class, Group Class, GroupCollection Class, Match Class, MatchCollection Class.
A regular expression pattern is considered as the zero natural Group. A bracket regular expression pattern is a defined Group. All groups are collected as a GroupCollection. Capture is the basic captured unit of regular expressions for both Match and Group to represent a single successful subexpression capture. The Capture of one or more occurrences of a bracket group is collected as a MatchCollection. However, the MatchCollection is defined as the set of Captures made by a sngle capturing Group.
Capture Class
Represents the results from a single successful subexpression capture.
The position in the original string where the first character of the captured substring is found.
Length
Gets the length of the captured substring.
Value
Gets the captured substring from the input string.
Methods
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)
ToString()
Retrieves the captured substring from the input string by calling the Value property.
Remarks
A Capture object is immutable and has no public constructor. Instances are returned through the CaptureCollection object, which is returned by the Match.Captures and Group.Captures properties. However, the Match.Captures property provides information about the same match as the Match object.
If you do not apply a quantifier to a capturing group, the Group.Captures property returns a CaptureCollection with a single Capture object that provides information about the same capture as the Group object. If you do apply a quantifier to a capturing group, the Group.Index, Group.Length, and
Group.Value properties provide information only about the last captured group, whereas the Capture objects in the CaptureCollection provide information about all subexpression captures. The example provides an illustration.
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
Properties
Count
Gets the number of substrings captured by the group.
IsReadOnly
Gets a value that indicates whether the collection is read only.
IsSynchronized
Gets a value that indicates whether access to the collection is synchronized (thread-safe).
Item[Int32]
Gets an individual member of the collection.
SyncRoot
Gets an object that can be used to synchronize access to the collection.
Methods
CopyTo(Array, Int32)
Copies all the elements of the collection to the given array beginning at the given index.
Equals(Object)
Determines whether the specified object is equal to the current object.
(Inherited from Object)
GetEnumerator()
Provides an enumerator that iterates through the collection.
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)
ToString()
Returns a string that represents the current object.
(Inherited from Object)
Extension Methods
Cast<TResult>(IEnumerable)
Casts the elements of an IEnumerable to the specified type.
OfType<TResult>(IEnumerable)
Filters the elements of an IEnumerable based on a specified type.
AsParallel(IEnumerable)
Enables parallelization of a query.
AsQueryable(IEnumerable)
Converts an IEnumerable to an IQueryable.
Remarks
The collection is immutable (read-only) and has no public constructor. The CaptureCollection object contains one or more Capture objects.
Instances of the CaptureCollection class are returned by the following properties:
The Group.Captures property. Each member of the collection represents a substring captured by a capturing group. If a quantifier is not applied to a capturing group, the CaptureCollection includes a single Capture object that represents the same captured substring as the Group object. If a quantifier is applied to a capturing group,
the CaptureCollection includes one Capture object for each captured substring, and the Group object provides information only about the last captured substring.
The Match.Captures property. In this case, the collection consists of a single Capture object that provides information about the match as a whole. That is, the CaptureCollection object provides the same information as the Match object.
To iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For Each…Next in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.
[System.Serializable]
public class Group : System.Text.RegularExpressions.Capture
Properties
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.
Index
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.
Success
Gets a value indicating whether the match is successful.
Value
Gets the captured substring from the input string.
(Inherited from Capture)
Methods
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)
Synchronized(Group)
Returns a Group object 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)
Remarks
A capturing group can capture zero, one, or more strings in a single match because of quantifiers. (For more information, see Quantifiers.) All the substrings matched by a single capturing group are available from the Group.Captures property. Information about the last substring captured can be accessed directly from the Value and Index properties. (That is, the Group instance is equivalent to the last item of the collection returned by the
Captures property, which reflects the last capture made by the capturing group.)
[System.Serializable]
public class GroupCollection : System.Collections.ICollection
Properties
Count
Returns the number of groups in the collection.
IsReadOnly
Gets a value that indicates whether the collection is read-only.
IsSynchronized
Gets a value that indicates whether access to the GroupCollection is synchronized (thread-safe).
Item[Int32]
Enables access to a member of the collection by integer index.
Item[String]
Enables access to a member of the collection by string index.
SyncRoot
Gets an object that can be used to synchronize access to the GroupCollection.
Methods
CopyTo(Array, Int32)
Copies all the elements of the collection to the given array beginning at the given index.
Equals(Object)
Determines whether the specified object is equal to the current object.
(Inherited from Object)
GetEnumerator()
Provides an enumerator that iterates through the collection.
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)
ToString()
Returns a string that represents the current object.
(Inherited from Object)
Extension Methods
Cast<TResult>(IEnumerable)
Casts the elements of an IEnumerable to the specified type.
OfType<TResult>(IEnumerable)
Filters the elements of an IEnumerable based on a specified type.
AsParallel(IEnumerable)
Enables parallelization of a query.
AsQueryable(IEnumerable)
Converts an IEnumerable to an IQueryable.
Remarks
The GroupCollection class is a zero-based collection class that consists of one or more Group objects that provide information about captured groups in a regular expression match. The collection is immutable (read-only) and has no public constructor. A GroupCollection object is returned by the Match.Groups property.
The collection contains one or more System.Text.RegularExpressions.Group objects. If the match is successful, the first element in the collection contains the Group object that corresponds to the entire match. Each subsequent element represents a captured group, if the regular expression includes capturing groups. Matches from numbered (unnamed) capturing groups appear in numeric order before matches from named capturing groups. If the match is unsuccessful, the collection contains a single System.Text.RegularExpressions.Group object whose Success property is false and whose Value property equals String.Empty. For more information, see the "Grouping Constructs and Regular Expression Objects" section in the Grouping Constructs article.
To iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For Each…Next in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method. In addition, you can access individual numbered captured groups from the Item[Int32] property (the indexer in C#), and you can access individual named captured groups from the
Item[String] property. Note that you can retrieve an array that contains the numbers and names of all capturing groups by calling the Regex.GetGroupNumbers and Regex.GetGroupNames methods, respectively. Both are instance methods and require that you instantiate a
Regex object that represents the regular expression to be matched.