Get which capture group matched a result using Delphi's TRegex

Internally Delphi uses class TPerlRegEx and it has such description for GroupCount property:

Number of matched groups stored in the Groups array. This number is the number of the highest-numbered capturing group in your regular expression that actually participated in the last match. It may be less than the number of capturing groups in your regular expression.

E.g. when the regex "(a)|(b)" matches "a", GroupCount will be 1. When the same regex matches "b", GroupCount will be 2.

TRegEx class always adds one more group (for whole expression i guess). In your case it should be enough to check every match like this:

case Match.Groups.Count-1 of
  1: ; // "word" found
  2: ; // "id" found
  3: ; // "course" found
end;

It doesn't answer why Groups are filled with strange data, indeed it seems to be enough to answer your question. :)

Tags:

Regex

Delphi