Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

GroupCollection::Item Property (Int32)

 

Enables access to a member of the collection by integer index.

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

public:
property Group^ default[
	int groupnum
] {
	Group^ get(int groupnum);
}

Parameters

groupnum
Type: System::Int32

The zero-based index of the collection member to be retrieved.

Property Value

Type: System.Text.RegularExpressions::Group^

The member of the collection specified by groupnum.

The Item[Int32] property is the index (in C#) or the default property (in Visual Basic) of the GroupCollection class. It allows you to enumerate the members of the collection by using a foreach statement in C# or a For Each statement in Visual Basic.

You can also use this property to retrieve individual captured groups by their index number. You can retrieve an array that contains the numbers of all capturing groups in a regular expression by calling the instance Regex::GetGroupNumbers method. You can also map named capturing groups to their numbers by calling the instance Regex::GroupNumberFromName method.

You can determine the number of items in the collection by retrieving the value of the Count property. Valid values for the groupnum parameter range from 0 to one less than the number of items in the collection.

The GroupCollection object returned by the Match::Groups property always has at least one member. If the regular expression engine cannot find any matches in a particular input string, the single Group object in the collection has its Group::Success property set to false and its Group.Value property set to String::Empty.

If groupnum is not the index of a member of the collection, or if groupnum is the index of a capturing group that has not been matched in the input string, the method returns a Group object whose Group::Success property is false and whose Group.Value property is String::Empty.

The following example defines a regular expression that consists of two numbered groups. The first group captures one or more consecutive digits. The second group matches a single character. Because the regular expression engine looks for zero or one occurrence of the first group, it does not always find a match even if the regular expression match is successful. The example then illustrates the result when the Item[Int32] property is used to retrieve an unmatched group, a matched group, and a group that is not defined in the regular expression. The example defines a regular expression pattern (\d+)*(\w)\2, which is interpreted as shown in the following table.

Pattern

Description

(\d+)*

Match one or more occurrence of a decimal digit. This is the first capturing group. Match this pattern either zero or one time.

(\w)

This is the second capturing group.

\k

Match the string captured by the second capturing group.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft