andgineer / TRegExpr

Regular expressions (regex), pascal.
https://regex.sorokin.engineer/en/latest/
MIT License
174 stars 63 forks source link

RegExp.ModifierS does not work properly #277

Closed Uefi1 closed 1 year ago

Uefi1 commented 2 years ago

Hello, RegExp.ModifierS grabs extra lines

var RegExp: TRegExpr; begin RegExp := TRegExpr.Create; RegExp.ModifierS := true; RegExp.ModifierM:=true;

https://regex101.com/r/hDp6q8/1

Delphi: lenin 3: money 4: this

Alexey-T commented 2 years ago
  1. maybe it is your error - if modifierS is ON, dot finds more chars (also finds NewLine)
  2. give the compilable example for FreePascal
Uefi1 commented 2 years ago

2. give the compilable example for FreePascal

function pars(n:integer;RegString : AnsiString; thmlcode: AnsiString):TStringList; var RegExp: TRegExpr; begin RegExp := TRegExpr.Create; RegExp.ModifierS := true; RegExp.ModifierM:=true; RegExp.InputString := thmlcode; RegExp.Expression := RegString; try try if RegExp.Exec then repeat textmp.Add(RegExp.Match[n]); until RegExp.ExecNext=false; except end; finally RegExp.Free; end; result:=textmp; end;

procedure TForm1.Button2Click(Sender: TObject); begin Base:=Tstringlist.Create; Base.Clear; if opendialog1.Execute then Base.LoadFromFile(Opendialog1.FileName); end;

procedure TForm1.Button1Click(Sender: TObject); begin textmp:=TStringList.Create; memo1.lines.AddStrings(pars(1,'2:\s(.+)\s3:\s(.+)',Base.text)); freeandnil(textmp); end;

Alexey-T commented 2 years ago

Please attach the zip file with needed files- .lpr .pas .lfm .lpi files.

Uefi1 commented 2 years ago

Please attach the zip file https://dropmefiles.com/ihehD

Alexey-T commented 2 years ago

I made Linux app now

Screenshot from 2022-07-01 16-47-03

how to see error here??

Uefi1 commented 2 years ago

how to see error here??

No errors, parses the word incorrectly create .txt from words and openned it:

1: batman 2: lenin 3: money 4: this

Uefi1 commented 2 years ago

lenin? Are you trolling here? maybe hitler too?

Words are not important, they are taken in guesswork

Alexey-T commented 2 years ago

I tested on my file. I see it is OK .

if ModifierS=True, group[2] takes all text from "3: " until end of file. if ModifierS=False, group[2] takes only one line after "3:"

Lazarus/ Linux

Uefi1 commented 2 years ago

I tested on my file. I see it is OK .

Sorry try with this regex: https://regex101.com/r/xSf83d/1

And This is Words in text file:

1: batman 2: lenin 3: money 4: this

1: superman 2: rabbit
3: deep 4: thanks

AmigoJack commented 2 years ago

@Uefi1 Your Pascal code sets the modifiers S, G and M (S and G are on by default). Your regex101 code sets the modifiers G, M and I. And not S. Add S to it, otherwise you're comparing apples with oranges.

Alexey-T commented 1 year ago

@andgineer Seems user mis-tests the S regex modifier. seems all is OK in the code. let's close?