Recent

Author Topic: SubExprMatchCount = 0  (Read 954 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 258
SubExprMatchCount = 0
« on: November 23, 2019, 02:50:09 pm »
slOut should end up with 3 items, instead there are none.
I'm pretty sure this code was working, now it does not
Code: Pascal  [Select][+][-]
  1. var
  2.   re: TRegExpr;
  3.   UserString: String;
  4.   slOut: TStringList;
  5.   i: integer;
  6. begin
  7.   slOut := TStringList.Create();
  8.   re := TRegExpr.Create();
  9.   re.InputString :='AAA'
  10.   re.Expression := 'A';
  11.  
  12.  
  13.  
  14.   if re.Exec(1) then
  15.   repeat
  16.     if re.SubExprMatchCount > -1 then
  17.     begin
  18.     showmessage(IntToStr(re.SubExprMatchCount));// --> SubExprMatchCount = 0
  19.     i :=0;
  20.       for i := 0 to re.SubExprMatchCount -1 do
  21.       begin
  22.         slOut.Add(re.Match[i]);
  23.       end;
  24.     end;
  25.   until not re.ExecNext;
  26.  
  27.  
« Last Edit: November 23, 2019, 02:54:19 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: SubExprMatchCount = 0
« Reply #1 on: November 23, 2019, 04:17:12 pm »
Your regex does not contain a sub expression. It is a single expression. What you wrote is like this:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses
  3.   regexpr;
  4. begin
  5.   with TRegExpr.Create('A') do { with for briefness }
  6.   try
  7.     InputString :='AAA';
  8.     if Exec then repeat
  9.         writeln(Match[0]);
  10.     until not ExecNext;    
  11.   finally
  12.     Free;
  13.   end;
  14. end.

Do you really mean subexpression or do you mean matchcount and matches?

Using subexpressions it would look something like this:
Code: Pascal  [Select][+][-]
  1. program testitagain;
  2. {$mode objfpc}
  3. uses
  4.   regexpr;
  5. var
  6.   i:integer;
  7. begin
  8.   with TRegExpr.Create('(A)?(A)?(A)?') do  // now we have a subexpression that looks for triple A's
  9.   try
  10.     InputString :='AAA';
  11.     if Exec then
  12.     begin
  13.       writeln('This is the match: ', Match[0]);
  14.       repeat
  15.       if SubExprMatchCount > 0 then
  16.           for i := 1 to SubExprMatchCount do
  17.             writeln(i, ' sub: ',Match[i]);
  18.       until not execnext;
  19.     end;
  20.   finally
  21.     Free;
  22.   end;
  23. end.
Output:
Code: Bash  [Select][+][-]
  1. This is the match: A
  2. 1 sub: A
  3. 2 sub: A
  4. 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.


« Last Edit: November 23, 2019, 07:57:58 pm by Thaddy »
Specialize a type, not a var.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 258
Re: SubExprMatchCount = 0
« Reply #2 on: November 23, 2019, 08:51:49 pm »
Right. Thanks.
lazarus 3.2-fpc-3.2.2-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: SubExprMatchCount = 0
« Reply #3 on: November 23, 2019, 09:17:16 pm »
Right. Thanks.
You're welcome. Note there are a few people on this forum (few, but much better!) who are better in the dark art of regular expressions than I am, so there may be a follow up.
« Last Edit: November 23, 2019, 09:24:04 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018