Recent

Author Topic: Regex  (Read 999 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 267
Regex
« on: June 16, 2019, 07:45:30 am »
Can't see why this regex is not found ?
Code: Pascal  [Select][+][-]
  1. var
  2.   re: TRegExpr;
  3. begin
  4.   re := TRegExpr.Create('<foo>foo</foo>');
  5.   if re.Exec('>.*?<') then
  6.   begin
  7.     WriteLn(re.Match[1]);
  8.     while re.ExecNext do
  9.     begin
  10.       WriteLn(re.Match[1]);
  11.     end;
  12.   end;
  13.   re.Free;
  14. end;  
  15.  
lazarus 3.2-fpc-3.2.2-win32/win64

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Regex
« Reply #1 on: June 16, 2019, 09:10:20 am »
Your syntax needs help  :o

Try:

Code: Pascal  [Select][+][-]
  1.     re := TRegExpr.Create;
  2.     re.Expression := '>.*?<';
  3.     if re.Exec('<foo>foo</foo>') then
  4.     begin
  5.       WriteLn(re.Match[0]);
  6.       while re.ExecNext do
  7.       begin
  8.         WriteLn(re.Match[1]);
  9.       end;
  10.     end;
  11.     re.Free;  
  12.  
 
« Last Edit: June 16, 2019, 09:11:51 am by trev »

Thaddy

  • Hero Member
  • *****
  • Posts: 14392
  • Sensorship about opinions does not belong here.
Re: Regex
« Reply #2 on: June 16, 2019, 09:26:35 am »
Also not correct, try this and  a bit shorter (except the try/finally I always add):
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses regexpr;
  3. var
  4.   re: TRegExpr;
  5. begin
  6.   re := TRegExpr.Create('>.*?<'); // NOT the string to search, but the expression
  7.   try
  8.     if re.Exec('<foo>foo</foo>bar<foo>') then // Here goes the string to process, not the expression.
  9.     begin
  10.       WriteLn(re.Match[0]); // index starts at zero, not one...
  11.       while re.ExecNext do  // to test the second match I added...
  12.         WriteLn(re.Match[0]);
  13.     end;
  14.   finally
  15.     re.Free;
  16.   end;
  17. end.

>If you don't mind with, even:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses regexpr;
  3. begin
  4.   with TRegExpr.Create('>.*?<') do // NOT the string to search, but the expression
  5.   try
  6.     if Exec('<foo>foo</foo>bar<foo>') then // Here goes the string to process, not the expression.
  7.     begin
  8.       WriteLn(Match[0]);  // index starts at zero, not one...
  9.       while ExecNext do   // to test the second match I added...
  10.         WriteLn(Match[0]);// index is still zero, not one or higher
  11.     end;
  12.   finally
  13.     Free;
  14.   end;
  15. end.
This is all well explained in the sourcecode.
« Last Edit: June 16, 2019, 10:33:55 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 267
Re: Regex
« Reply #3 on: June 16, 2019, 10:20:10 am »
Thanks.
lazarus 3.2-fpc-3.2.2-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14392
  • Sensorship about opinions does not belong here.
Re: Regex
« Reply #4 on: June 16, 2019, 10:47:38 am »
Maybe I should add that the match list is there for subexpressions. If you do not use them, always read from zero.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018