Regex.GetGroupNumbers Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns an array of capturing group numbers that correspond to group names in an array.

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

Syntax

'Declaration
Public Function GetGroupNumbers As Integer()
public int[] GetGroupNumbers()

Return Value

Type: array<System.Int32[]
An integer array of group numbers.

Remarks

Both unnamed and named capturing groups can be accessed by number. Unnamed groups are numbered from left to right starting with 1. (The capturing group in index 0 (zero) represents the match as a whole.) Named groups are then numbered from left to right starting with a number that is one greater than the number of unnamed capturing groups.

Referencing a group by its number instead of by string name can provide faster access.

Examples

The following example defines a regular expression, \b((?<word>\w+)\s*)+(?<end>[.?!]), that matches a sentence. The regular expression includes three capturing groups: an unnamed group that captures an individual word along with a space character that may follow it; a group named word that captures the individual words in the sentence; and a group named end that captures the punctuation that ends the sentence. The example calls the GetGroupNumbers method to get the numbers of all capturing groups, and then displays their captured string. In addition, the GroupNameFromNumber method is used to indicate whether a particular numbered group corresponds to a named group.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim pattern As String = "\b((?<word>\w+)\s*)+(?<end>[.?!])"
      Dim input As String = "This is a sentence. This is a second sentence."

      Dim rgx As New Regex(pattern)
      Dim groupNumbers() As Integer = rgx.GetGroupNumbers()
      Dim m As Match = rgx.Match(input)
      If m.Success Then
         outputBlock.Text += String.Format("Match: {0}", m.Value) & vbCrLf
         For Each groupNumber In groupNumbers
            Dim name As String = rgx.GroupNameFromNumber(groupNumber)
            Dim number As Integer
            outputBlock.Text += String.Format("   Group {0}{1}: '{2}'",  
                              groupNumber, 
                              If(Not String.IsNullOrEmpty(name) And 
                              Not Int32.TryParse(name, number),
                                 " (" + name + ")", String.Empty), 
                              m.Groups(groupNumber).Value) & vbCrLf
         Next
      End If
   End Sub
End Module
' The example displays the following output:
'       Match: This is a sentence.
'          Group 0: 'This is a sentence.'
'          Group 1: 'sentence'
'          Group 2 (word): 'sentence'
'          Group 3 (end): '.'
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string pattern = @"\b((?<word>\w+)\s*)+(?<end>[.?!])";
      string input = "This is a sentence. This is a second sentence.";

      Regex rgx = new Regex(pattern);
      int[] groupNumbers = rgx.GetGroupNumbers();
      Match m = rgx.Match(input);
      if (m.Success)
      {
         outputBlock.Text += String.Format("Match: {0}", m.Value) + "\n";
         foreach (var groupNumber in groupNumbers)
         {
            string name = rgx.GroupNameFromNumber(groupNumber);
            int number;
            outputBlock.Text += String.Format("   Group {0}{1}: '{2}'",
                              groupNumber,
                              !string.IsNullOrEmpty(name) &
                              !Int32.TryParse(name, out number) ?
                                 " (" + name + ")" : String.Empty,
                              m.Groups[groupNumber].Value) + "\n";
         }
      }
   }
}
// The example displays the following output:
//       Match: This is a sentence.
//          Group 0: 'This is a sentence.'
//          Group 1: 'sentence'
//          Group 2 (word): 'sentence'
//          Group 3 (end): '.'

The regular expression pattern is interpreted as shown in the following table.

Pattern

Description

\b

Begin the match at a word boundary.

(?<word>\w+)

Match one or more word characters and assign the matched strings to a group named word.

\s*

Match zero or more white-space characters.

((?<word>\w+)\s*)

Assign the word captured group followed by any captured white-space characters to the first captured group.

((?<word>\w+)\s*)+

Match the pattern of one or more word characters followed by any white-space characters one or more times.

(?<end>[.?!])

Match a period, question mark, or exclamation point. Assign the matched character to the end capturing group.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.