GetGroupNames Method

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:

Pattern

Description

\b

Begin the match at a word boundary.

(?<FirstWord>\w+)

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

\s?

Match zero or one white-space characters.

(\w+)

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

\s

Match a white-space character.

((\w+)\s)*

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

(?<LastWord>\w+)?

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

(?<Punctuation>\p{Po})

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

Universal Windows Platform
Available since 4.5
.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:
© 2016 Microsoft