Export (0) Print
Expand All

Regex.GetGroupNames Method

Returns an array of capturing group names for the regular expression.

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

public string[] GetGroupNames()

Return Value

Type: System.String[]
A string array of group names.

The collection of group names contains the set of strings used to name capturing groups in the expression. Even if capturing groups are not explicitly named, they are automatically assigned numerical names ("0", "1", "2", "3", and so on). The "0" named group represents all text matched by the regular expression pattern. Numbered groups precede explicitly named groups in the collection, and named groups appear in the order in which they are defined in the regular expression pattern.

You can use the Length property on the array returned by this method to determine the number of groups in a regular expression.

The following example defines a general-purpose ShowMatches method that displays the names of regular expression groups and their matched text.

using System;
using System.Text.RegularExpressions;

public class Example
   public static void Main()
      string pattern = @"\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})";
      string input = "The cow jumped over the moon.";
      Regex rgx = new Regex(pattern);
      Match match = rgx.Match(input);
      if (match.Success)
         ShowMatches(rgx, match);

   private static void ShowMatches(Regex r, Match m)
      string[] names = r.GetGroupNames();
      Console.WriteLine("Named Groups:");
      foreach (var name in names) {
         Group grp = m.Groups[name];
         Console.WriteLine("   {0}: '{1}'", name, grp.Value);
// The example displays the following output: 
//       Named Groups: 
//          0: 'The cow jumped over the moon.' 
//          1: 'the ' 
//          2: 'the' 
//          FirstWord: 'The' 
//          LastWord: 'moon' 
//          Punctuation: '.'

In this case, the regular expression pattern \b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po}) is intended to parse a simple sentence, and to identify its first word, last word, and ending punctuation mark. The following table shows how the regular expression pattern is interpreted:




Begin the match at a word boundary.


Match one or more word characters. This is the FirstWord named group.


Match zero or one white-space characters.


Match one or more word characters. This is the second capturing group.


Match a white-space character.


Match zero or more occurrences of one or more word characters followed by a white space. This is the first capturing group.


Match zero or one occurrence of one or more word characters. This is the LastWord named group.


Match a character whose Unicode category is Punctuation, Other. This is the Punctuation named group.

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8

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.

© 2014 Microsoft