Regex.GetGroupNames Method ()

 

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

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

Public Function GetGroupNames As String()

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.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(?<FirstWord>\w+)\s?((\w+)\s)*(?<LastWord>\w+)?(?<Punctuation>\p{Po})"
      Dim input As String = "The cow jumped over the moon."
      Dim rgx As New Regex(pattern)
      Dim match As Match = rgx.Match(input)
      If match.Success Then ShowMatches(rgx, match)
   End Sub

   Private Sub ShowMatches(r As Regex, m As Match)
      Dim names() As String = r.GetGroupNames()
      Console.WriteLine("Named Groups:")
      For Each name In names
         Dim grp As Group = m.Groups.Item(name)
         Console.WriteLine("   {0}: '{1}'", name, grp.Value)
      Next
   End Sub
End Module
' 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 8
.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: