Your regex does not contain a sub expression. It is a single expression. What you wrote is like this:
{$mode objfpc}
uses
regexpr;
begin
with TRegExpr.Create('A') do { with for briefness }
try
InputString :='AAA';
if Exec then repeat
writeln(Match[0]);
until not ExecNext;
finally
Free;
end;
end.
Do you really mean
subexpression or do you mean
matchcount and
matches?
Using subexpressions it would look something like this:
program testitagain;
{$mode objfpc}
uses
regexpr;
var
i:integer;
begin
with TRegExpr.Create('(A)?(A)?(A)?') do // now we have a subexpression that looks for triple A's
try
InputString :='AAA';
if Exec then
begin
writeln('This is the match: ', Match[0]);
repeat
if SubExprMatchCount > 0 then
for i := 1 to SubExprMatchCount do
writeln(i, ' sub: ',Match[i]);
until not execnext;
end;
finally
Free;
end;
end.
Output:
This is the match: A
1 sub: A
2 sub: A
3 sub: A
"Look for 'A" and how many 'A''s are there?
Note that the expression can be shortened to just (A)? but that would print subindexes which are all 1, not 1,2,3. and there are better solutions: this to demo subexpressions.
If you need further explanation let me know.