Recent

Author Topic: search regex [solved]  (Read 721 times)

rnervi

  • New Member
  • *
  • Posts: 28
search regex [solved]
« on: September 01, 2025, 07:52:13 am »
I need to find this case in a sheet:
is good if inside two slash there are 3 chars  e.g. "AA/ABC/222"

so I built a regex:

Code: Pascal  [Select][+][-]
  1. MySearchParams.SearchText := '~\/[a-z]{3}\/~gmi';
  2. MySearchParams.Options := [soEntireDocument, soCompareEntireCell, soRegularExpr];
  3. MySearchParams.Within := swWorkbook;

that in testfield (regex) it works

but seems ignored in the

 IF FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) THEN

(no match)

any ideas ?
ty
« Last Edit: September 01, 2025, 09:15:16 pm by rnervi »

wp

  • Hero Member
  • *****
  • Posts: 13195
Re: search regex
« Reply #1 on: September 01, 2025, 10:37:12 am »
FPSpreadsheet uses the regex coming with FPC (unit RegExpr), and that does not find it:
Code: Pascal  [Select][+][-]
  1. uses
  2.   RegExpr;
  3.  
  4.   procedure RunRegEx;
  5.   var
  6.     re: TRegExpr;
  7.   begin
  8.     re := TRegExpr.Create('~\/[a-z]{3}\/~gmi');
  9.     if re.Exec('AA/ABC/222') then
  10.     begin
  11.       WriteLn(re.Match[1]);
  12.       while re.ExecNext do
  13.       begin
  14.         WriteLn(re.Match[1]);
  15.       end;
  16.     end else
  17.       WriteLn('-- not found --');
  18.     re.Free;
  19.   end;
  20.  
  21. begin
  22.   RunRegEx;
  23.   ReadLn;
  24. end.

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: search regex
« Reply #2 on: September 01, 2025, 10:58:26 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Sysutils, Classes, regexpr;
  4. Var
  5.   r:TRegExpr;
  6.   b:Boolean;
  7.  
  8. begin
  9.   r:=TRegexpr.Create;
  10.   r.Expression:='\/[a-z]{3}\/';
  11.   r.ModifierG:=True;
  12.   r.ModifierI:=True;
  13.   r.ModifierM:=True;
  14.   r.InputString:='AA/ABC/222';
  15.   b:=r.Exec;
  16.   IF b Then Writeln('Found') Else Writeln('not found');
  17.   Readln;
  18. end.
Writes "Found"

Conclusion: Leave out the Tildes, and set the Modifiers as shown

EDIT:
I could circumvent the "i" and "m" Modifiers, but haven't found how to avoid the "g"-Modifier
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Sysutils, Classes, regexpr;
  4. Var
  5.   r:TRegExpr;
  6.   b:Boolean;
  7.  
  8. begin
  9.   r:=TRegexpr.Create;
  10.   r.Expression:='(?i)(?m)\/[a-z]{3}\/';
  11.   r.ModifierG:=True;
  12.   //r.ModifierI:=True;
  13.   //r.ModifierM:=True;
  14.   r.InputString:='AA/ABC/222';
  15.   b:=r.Exec;
  16.   IF b Then Writeln('Found') Else Writeln('not found');
  17.   Readln;
  18. end.
Returns "Found"

EDIT2: re "g"-Modifier
Seems there is no "Pattern"-Modifier for "g".
It's just the difference finding the first Match, or going through a loop finding all

OTOH, i wouldn't know whats sense it makes to find all occurences IN ONE GO in a spreadsheet.
It's always been "stop after first match", and "continue search" if it's not the match you are looking for
« Last Edit: September 01, 2025, 11:15:55 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18303
  • Here stood a man who saw the Elbe and jumped it.
Re: search regex
« Reply #3 on: September 01, 2025, 11:49:19 am »
I used the simpler /[A-Za-z]{3}\/
Dunno if that is enough
« Last Edit: September 01, 2025, 12:05:13 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: search regex
« Reply #4 on: September 01, 2025, 12:22:50 pm »
I used the simpler /[A-Za-z]{3}\/
Dunno if that is enough
You'd still need the modifier for Multiline
my sample above works with TRegExpr
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rnervi

  • New Member
  • *
  • Posts: 28
Re: search regex
« Reply #5 on: September 01, 2025, 08:43:22 pm »
ty to all will try and let know results

@Thaddy  : works like a charm
« Last Edit: September 01, 2025, 09:10:22 pm by rnervi »

 

TinyPortal © 2005-2018