Condividi tramite


GroupCollection.Item Property (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: August 2009

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

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

Syntax

'Declaration
Public ReadOnly Property Item ( _
    groupname As String _
) As Group
public Group this[
    string groupname
] { get; }

Parameters

Property Value

Type: System.Text.RegularExpressions.Group
The member of the collection specified by groupname.

Remarks

groupName can be either the name of a capturing group that is defined by the (?<name>) element in a regular expression, or the string representation of the number of a capturing group that is defined by a grouping construct.

If groupname is not the name of a capturing group in the collection, or if groupname is the name 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.

Examples

The following example defines a regular expression that consists of two named groups. The first group, numbers, captures one or more consecutive digits. The second group, letter, matches a single character. Because the regular expression engine looks for zero or one occurrence of the pattern defined by the numbers group, the numbers group is not always present even if a match is successful. The example then illustrates the result when the Item[String] 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 (?<numbers>\d+)*(?<letter>\w)\k<letter>, which is interpreted as shown in the following table.

Pattern

Description

(?<numbers>\d+)*

Match one or more occurrence of a decimal digit. Name this the numbers capturing group. Match this pattern either zero or one time.

(?<letter>\w)

Match a single word character. Name this the letter capturing group.

\k<letter>

Match the string captured by the letter capturing group.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "(?<numbers>\d+)*(?<letter>\w)\k<letter>"
      Dim input As String = "AA"
      Dim match As Match = Regex.Match(input, pattern)

      ' Get the first named group.
      Dim group1 As Group = match.Groups.Item("numbers")
      outputBlock.Text += String.Format("Group 'numbers' value: {0}", If(group1.Success, group1.Value, "Empty")) & vbCrLf

      ' Get the second named group.
      Dim group2 As Group = match.Groups.Item("letter")
      outputBlock.Text += String.Format("Group 'letter' value: {0}", If(group2.Success, group2.Value, "Empty")) & vbCrLf

      ' Get a non-existent group.
      Dim group3 As Group = match.Groups.Item("none")
      outputBlock.Text += String.Format("Group 'none' value: {0}", If(group3.Success, group2.Value, "Empty")) & vbCrLf
   End Sub
End Module
' The example displays the following output:
'       Group 'numbers' value: Empty
'       Group 'letter' value: A
'       Group 'none' value: Empty
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string pattern = @"(?<numbers>\d+)*(?<letter>\w)\k<letter>";
      string input = "AA";
      Match match = Regex.Match(input, pattern);

      // Get the first named group.
      Group group1 = match.Groups["numbers"];
      outputBlock.Text += String.Format("Group 'numbers' value: {0}", group1.Success ? group1.Value : "Empty") + "\n";

      // Get the second named group.
      Group group2 = match.Groups["letter"];
      outputBlock.Text += String.Format("Group 'letter' value: {0}", group2.Success ? group2.Value : "Empty") + "\n";

      // Get a non-existent group.
      Group group3 = match.Groups["none"];
      outputBlock.Text += String.Format("Group 'none' value: {0}", group3.Success ? group2.Value : "Empty") + "\n";
   }
}
// The example displays the following output:
//       Group 'numbers' value: Empty
//       Group 'letter' value: A
//       Group 'none' value: Empty

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

August 2009

Expanded the Remarks section and added an example.

Customer feedback.