Recent

Author Topic: Regex  (Read 420 times)

BubikolRamios

  • Full Member
  • ***
  • Posts: 239
Regex
« on: June 30, 2022, 01:56:07 am »
Text;
Quote
1.       Phocides pigmalion (Cramer, 1779)
foo
a.   Phocides pigmalion okeechobee (Worthington, 1881)

some external editor highligts 1. and 3 . line  as match to regex  '(^[0-9]+\..*$)|(^[a-z]\.+.*$)'. Expected.


This repeat should go thru twice, instead goes only once and put out single line containig entire text
Code: Pascal  [Select][+][-]
  1.   re.Expression:= '(^[0-9]+\..*$)|(^[a-z]\.+.*$)';
  2.   re.InputString:= 'the upper text';
  3.  
  4.   if re.Exec(1) then
  5.   repeat
  6.     strinList.Add(re.Match[0]);
  7.   until not re.ExecNext;
  8.  
  9.  

The problem is in regex as I see it, if the text was

Quote
a foo a foo ..
foo a
and regex 'a' it would work, output would be 3 lines of a.

Any tip?
« Last Edit: June 30, 2022, 10:25:27 am by BubikolRamios »
lazarus-2.0.12-fpc-3.2.0-win32/win64

Roland57

  • Sr. Member
  • ****
  • Posts: 287
Re: Regex
« Reply #1 on: June 30, 2022, 06:55:33 am »
Hello!

If I understand well, the problem is that ^ and $ mean "start of the input text" and "end of the input text" (and not start and end of the line).

Another problem is that .* is too wide.

So one possibility would be to remove ^, to replace $ with \n, and to use .*? instead of .*.

Code: Pascal  [Select][+][-]
  1.  '([0-9]+\..*?\n)|([a-z]\.+.*?\n)'

Or better (IMHO), we could use, instead of ., a more restrictive class, for example [^\n].

Code: Pascal  [Select][+][-]
  1. '([0-9]+\.[^\n]*)|([a-z]\.+[^\n]*)'

Hope this helps.




AlexTP

  • Hero Member
  • *****
  • Posts: 1846
    • UVviewsoft
Re: Regex
« Reply #2 on: June 30, 2022, 08:44:34 am »
re.ModifierM:= True (of False?). This toggles behaviour for ^ and $ metachars.

BubikolRamios

  • Full Member
  • ***
  • Posts: 239
Re: Regex
« Reply #3 on: June 30, 2022, 09:54:37 am »
Was pushing that forward and backward, true, false, even re.ModifierStr:='m', No effect at all.
lazarus-2.0.12-fpc-3.2.0-win32/win64

AlexTP

  • Hero Member
  • *****
  • Posts: 1846
    • UVviewsoft
Re: Regex
« Reply #4 on: June 30, 2022, 10:04:11 am »
Ok, can you post small compilable example? with exact InputString. I will see it.
Tell what you expect from example.

BubikolRamios

  • Full Member
  • ***
  • Posts: 239
Re: Regex
« Reply #5 on: June 30, 2022, 10:22:28 am »
ZIP
lazarus-2.0.12-fpc-3.2.0-win32/win64

BubikolRamios

  • Full Member
  • ***
  • Posts: 239
Re: Regex
« Reply #6 on: June 30, 2022, 10:34:19 am »

Another problem is that .* is too wide.


OK, that.
So '.*?' instead
With modifierM = true;

No difference '.*' to '.*?' in external highlighter, though.

Thanks.
lazarus-2.0.12-fpc-3.2.0-win32/win64

AlexTP

  • Hero Member
  • *****
  • Posts: 1846
    • UVviewsoft
Re: Regex
« Reply #7 on: June 30, 2022, 11:51:35 am »
Solved. You forgot to clear re.ModifierS.
Must be
Code: Pascal  [Select][+][-]
  1.   re := TRegExpr.Create();
  2.  
  3.   re.ModifierM:= true;
  4.   re.ModifierS:= false;
  5.   re.Expression:= UserRegex;
  6.   re.Compile;
  7.   re.InputString:= SynEdit1.Text;
  8.  
  9.   if re.Exec(1) then
  10.   repeat
  11.     showmessage('match:'#10+re.Match[0]);
  12.     //slOut.Add(re.Match[0]);
  13.   until not re.ExecNext;
  14.  

 

TinyPortal © 2005-2018