This documentation is archived and is not being maintained.

Regex.GetGroupNumbers Method

Updated: December 2010

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

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

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

Return Value

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

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.

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.

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

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.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

December 2010

Expanded the Remarks section and added an example.

Customer feedback.

Show: