Regex::GetGroupNames Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

array<String^>^ GetGroupNames()

Return Value

Type: array<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.

No code example is currently available or this language may not be supported.

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top