Export (0) Print
Expand All

Regex.GroupNameFromNumber Method

Updated: December 2010

Returns the group name that corresponds to the specified group number.

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

public string GroupNameFromNumber(
	int i
)

Parameters

i
Type: System.Int32

The group number to convert to the corresponding group name.

Return Value

Type: System.String
A string that contains the group name associated with the specified group number. If there is no group name that corresponds to i, the method returns String.Empty.

A regular expression pattern may contain either named or numbered capturing groups, which delineate subexpressions within a pattern match. Numbered groups are delimited by the syntax (subexpression) and are assigned numbers based on their order in the regular expression. Named groups are delimited by the syntax (?<name>subexpression) or (?'name'subexpression), where name is the name by which the subexpression will be identified. (For more information, see Grouping Constructs.) The GroupNameFromNumber method identifies both named groups and numbered groups by their ordinal positions in the regular expression. Ordinal position zero always represents the entire regular expression. All numbered groups are then counted before named groups, regardless of their actual position in the regular expression pattern.

If i is the number of a named group, the method returns the name of the group. If i is the number of an unnamed group, the method returns the string representation of the number. For example, if i is 1, the method returns "1". If i is not the number of a capturing group, the method returns String.Empty.

If a pattern match is successful, the value returned by this method can then be used to retrieve the Group object that represents the captured group from the GroupCollection.Item property. The GroupCollection object is returned by the Match.Groups property.

The following example defines a regular expression pattern that matches an address line containing a U.S. city name, state name, and zip code. The example uses the GroupNameFromNumber method to retrieve the names of capturing groups. It then uses these names to retrieve the corresponding captured groups for successful matches.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)";
      string[] cityLines = {"New York, NY 10003", "Brooklyn, NY 11238", "Detroit, MI 48204", 
                            "San Francisco, CA 94109", "Seattle, WA 98109" };
      Regex rgx = new Regex(pattern);
      List<string> names = new List<string>();
      int ctr = 1;
      bool exitFlag = false;
      // Get group names. 
      do {
         string name = rgx.GroupNameFromNumber(ctr);
         if (! String.IsNullOrEmpty(name))
         {
            ctr++;
            names.Add(name);
         }
         else
         {
            exitFlag = true;
         }
      } while (! exitFlag);

      foreach (string cityLine in cityLines)
      {
         Match match = rgx.Match(cityLine);
         if (match.Success)
            Console.WriteLine("Zip code {0} is in {1}, {2}.", 
                               match.Groups[names[3]], 
                               match.Groups[names[1]], 
                               match.Groups[names[2]]);
      } 
   }
}
// The example displays the following output: 
//       Zip code 10003 is in New York, NY. 
//       Zip code 11238 is in Brooklyn, NY. 
//       Zip code 48204 is in Detroit, MI. 
//       Zip code 94109 is in San Francisco, CA. 
//       Zip code 98109 is in Seattle, WA.

The regular expression pattern is defined by the following expression:

(?<city>[A-Za-z\s]+), (?<state>[A-Za-z]{2}) (?<zip>\d{5}(-\d{4})?)

The following table shows how the regular expression pattern is interpreted.

Pattern

Description

(?<city>[A-Za-z\s]+)

Match one or more alphabetic or white-space character. Assign this captured group the name city.

,

Match a comma (,) followed by a white-space character.

(?<state>[A-Za-z]{2})

Match two alphabetic characters. Assign this captured group the name state. This group should be followed by a white-space character.

(?<zip>\d{5}(-\d{4})?)

Match five numeric digits followed by either zero or one occurrence of a hyphen followed by four digits. Assign this captured group the name zip.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

December 2010

Added more information about the return value to the Remarks section.

Customer feedback.

May 2009

Revised extensively.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft