Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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 Group this[
	int groupnum
] { get; }

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.

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.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\d+)*(\w)\2";
      string input = "AA";
      Match match = Regex.Match(input, pattern);

      // Get the first named group.
      Group group1 = match.Groups[1];
      Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");

      // Get the second named group.
      Group group2 = match.Groups[2];
      Console.WriteLine("Group 2 value: {0}", group2.Success ? group2.Value : "Empty");

      // Get a non-existent group.
      Group group3 = match.Groups[3];
      Console.WriteLine("Group 3 value: {0}", group3.Success ? group3.Value : "Empty");
   }
}
// The example displays the following output: 
//       Group 1 value: Empty 
//       Group 2 value: A 
//       Group 3 value: Empty

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.