Frank Burleigh
Frank Burleigh

Reputation: 311

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

I've written a regex whose job is to return all matches to its three alternative capture groups. My goal is to learn which capture group produced each match. PCRE seems able to produce that information. But I haven't yet been able coerce the TRegEx class in Delphi XE8 to yield meaningful capture group info for matches. I can't claim to be at the head of regex class, and TRegEx is new to me, so who knows what errors I'm making.

The regex (regex101.com workpad) is:

(?'word'\b[a-zA-Z]{3,}\b)|(?'id'\b\d{1,3}\b)|(?'course'\b[BL]\d{3}\b)

This test text:

externship L763 clinic 207 B706 b512

gives five matches in test environments. But a simple test program that walks the TGroupCollection of each TMatch in the TMatchCollection shows bizarre results about groups: all matches have more than one group (2, 3 or 4) with each group's Success true, and often the matched text is duplicated in several groups or is empty. So this data structure (below) isn't what I'd expect:

Using TRegEx
Regex: (?'word'\b[a-zA-Z]{3,}\b)|(?'id'\b\d{1,3}\b)|(?'course'\b[BL]\d{3}\b)
Text: externship L763 clinic 207 B706 b512

5 matches
 'externship' with 2 groups:
    length 10 at 1 value 'externship' (Sucess? True)
    length 10 at 1 value 'externship' (Sucess? True)
 'L763' with 4 groups:
    length 4 at 12 value 'L763' (Sucess? True)
    length 0 at 1 value '' (Sucess? True)
    length 0 at 1 value '' (Sucess? True)
    length 4 at 12 value 'L763' (Sucess? True)
 'clinic' with 2 groups:
    length 6 at 17 value 'clinic' (Sucess? True)
    length 6 at 17 value 'clinic' (Sucess? True)
 '207' with 3 groups:
    length 3 at 24 value '207' (Sucess? True)
    length 0 at 1 value '' (Sucess? True)
    length 3 at 24 value '207' (Sucess? True)
 'B706' with 4 groups:
    length 4 at 28 value 'B706' (Sucess? True)
    length 0 at 1 value '' (Sucess? True)
    length 0 at 1 value '' (Sucess? True)
    length 4 at 28 value 'B706' (Sucess? True)

My simple test runner is this:

program regex_tester;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils,
  System.RegularExpressions,
  System.RegularExpressionsCore;

var
  Matched     : Boolean;
  J           : integer;
  Group       : TGroup;
  Match       : TMatch;
  Matches     : TMatchCollection;
  RegexText,
  TestText    : String;
  RX          : TRegEx;
  RXPerl      : TPerlRegEx;

begin
  try
    RegexText:='(?''word''\b[a-zA-Z]{3,}\b)|(?''id''\b\d{1,3}\b)|(?''course''\b[BL]\d{3}\b)';
    TestText:='externship L763 clinic 207 B706 b512';

    RX:=TRegex.Create(RegexText);

    Matches:=RX.Matches(TestText);

    Writeln(Format(#10#13#10#13'Using TRegEx'#10#13'Regex: %s'#10#13'Text: %s'#10#13,[RegexText, TestText]));

    Writeln(Format('%d matches', [Matches.Count]));
    for Match in Matches do
    begin
      Writeln(Format(' ''%s'' with %d groups:', [Match.Value,Match.Groups.Count]));

      for Group in Match.Groups do
        Writeln(Format(#9'length %d at %d value ''%s'' (Sucess? %s)', [Group.Length,Group.Index,Group.Value,BoolToStr(Group.Success, True)]));
    end;

    RXPerl:=TPerlRegEx.Create;
    RXPerl.Subject:=TestText;
    RXPerl.RegEx:=RegexText;

    Writeln(Format(#10#13#10#13'Using TPerlRegEx'#10#13'Regex: %s'#10#13'Text: %s'#10#13,[RXPerl.Regex, RXPerl.Subject]));

    Matched:=RXPerl.Match;
    if Matched then
    repeat
      begin
        Writeln(Format(' ''%s'' with %d groups:', [RXPerl.MatchedText,RXPerl.GroupCount]));
        for J:=1 to RXPerl.GroupCount do
          Writeln(Format(#9'length %d at %d, value ''%s''',[RXPerl.GroupLengths[J],RXPerl.GroupOffsets[J],RXPerl.Groups[J]]));

        Matched:=RXPerl.MatchAgain;
      end;
    until Matched=false;

  except
      on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
  end;
end.

I'd certainly appreciate a nudge in the right direction. If TRegEx is broken, I can of course use an alternative -- or I can let go of the perceived elegance of the solution, instead using three simpler tests to find the bits of info I need.

Added Information and Interpretation

As @andrei-galatyn notes, TRegEx uses TPerlRegEx for its work. So I added a section to my testing program (output below) where I experiment with that, too. It isn't as convenient to use as TRegEx, but its result is what it should be -- and without the problems of TRegEx's broken TGroup data structures. Whichever class I use, the last group's index (less 1 for TRegEx) is the capturing group I want.

Along the way I was reminded that Pascal arrays are often based on 1 rather than 0.

Using TPerlRegEx
Regex: (?'word'\b[a-zA-Z]{3,}\b)|(?'id'\b\d{1,3}\b)|(?'course'\b[BL]\d{3}\b)
Text: externship L763 clinic 207 B706 b512

 'externship' with 1 groups:
    length 10 at 1, value 'externship'
 'L763' with 3 groups:
    length 0 at 1, value ''
    length 0 at 1, value ''
    length 4 at 12, value 'L763'
 'clinic' with 1 groups:
    length 6 at 17, value 'clinic'
 '207' with 2 groups:
    length 0 at 1, value ''
    length 3 at 24, value '207'
 'B706' with 3 groups:
    length 0 at 1, value ''
    length 0 at 1, value ''
    length 4 at 28, value 'B706'

Upvotes: 16

Views: 3444

Answers (2)

Jeremy Hodge
Jeremy Hodge

Reputation: 1665

I realize this is 4 years too late, but for others who may search for this, here's what I found (in Delphi RIO at least, I have not tested earlier versions, however the source URL below says as of XE), however:

From https://www.regular-expressions.info/delphi.html (important bit is bold italic)

The TMatch record provides several properties with details about the match. Success indicates if a match was found. If this is False, all other properties and methods are invalid. Value returns the matched string. Index and Length indicate the position in the input string and the length of the match. Groups returns a TGroupCollection record that stores a TGroup record in its default Item[] property for each capturing group. You can use a numeric index to Item[] for numbered capturing groups, and a string index for named capturing groups.

So, if you NAME your matching group like this for example:

(?'MatchName'[^\/\?]*)

Which would match a string up until a / or ? character, you can then reference that capture group by name like this:

Matches := TRegEx.Matches(StrToMatch, RegexString, [ roExplicitCapture ] );
for Match in Matches do begin
  // ... maybe some logic here to determine if this is the match you want ...
  try
    whatGotMatched := Match.Groups['MatchName'].Value;
  except
    whatGotMatched := '';
  end;
end;

How exactly you get to your match groups in the Matches is dependent on how you structure your RegEx etc, how many matches you create, etc. I surround the Match.Groups['MatchName'].Value in a try-except block because if the match wasn't found, you will generate an index out of bounds error when accessing .Value since it will still say the match would have been at character X, for 0 characters. You could also check Match.Groups['MatchName'].Length for 0 before trying to access .Value to probably better results...but if you reference it by name, you don't have to decipher specifically what groups matched what, etc ... just ask for the named match you wanted, and if it matched, you will get it.

Edit: they reference to .Length to help verify before throwing an error about index out of bounds. However, if your search is constructed where a named search is not matched, and therefore not returned at all, you will get an error thrown for an invalid match name, so you may still may need to capture for that.

Upvotes: 0

Andrei Galatyn
Andrei Galatyn

Reputation: 3432

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

Upvotes: 6

Related Questions