Editare

Partajați prin


Regex.GetGroupNames Method

Definition

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

public:
 cli::array <System::String ^> ^ GetGroupNames();
public string[] GetGroupNames ();
member this.GetGroupNames : unit -> string[]
Public Function GetGroupNames () As String()

Returns

String[]

A string array of group names.

Examples

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: '.'
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.

Remarks

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.

Applies to

See also