match class : GroupCount getter

Description

Gets a number of groups.

Syntax

matchInstance.GroupCount

Arguments

None

Return value

ClassDescription
intA number of groups.

Sample code

1:

regex pattern = new regex("(\d)(\d)"); // There are groups in the regular expression.

2:

match firstMatch= pattern.MatchM("ww1245cc66"); // It matches to "12".

3:

int count = firstMatch.GroupCount; // The GroupCount is 3. "12", "1", and "2"

4:

// ---------------------------------------------------------------------------------

5:

pattern = new regex("\d\d"); // There is no group in the regular expression.

6:

firstMatch= pattern.MatchM("ww1245cc66"); // It matches to "12".

7:

count = firstMatch.GroupCount; // The GroupCount is 1. The matched substring is also group.

8:

// ---------------------------------------------------------------------------------

9:

firstMatch= pattern.MatchM("wwwwwww"); // It does not match.

10:

count = firstMatch.GroupCount; // Still, the GroupCount is 1.

Notes

It's a wrapper of the System.Text.RegularExpressions.Match.Groups.Count property.

If the match is successful, the entire matched substring is also recognized as one group, so GroupCount == 1 even if there is no group specified in the regular expression pattern.

Even if the match fails, GroupCount == 1.

Links for reference

Copyright © Cooker All rights reserved.