Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-12

regex unit doesn't find matches???

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I'm using the following regular expression: 'foob.*r' Calling MatchString() against each of the items in the stringlist, I expected it to match 3 items. Instead it only matches the first item and fails all the rest. Why???

--------------[  source code ]-------------------
  sl := TStringList.Create;
  sl.Add('1st foobr Item');
  sl.Add('2nd Item');
  sl.Add('3rd Item');
  sl.Add('4th foobaoeuaor Item');
  sl.Add('5th foobar Item');
  r := TRegexEngine.Create('foob.*r');
  x := 0;
  y := 0;
  for i := 0 to sl.Count-1 do
  begin
    if r.MatchString(sl.Strings[i], x, y) then
      writeln('match')
    else
      writeln('failed');
  end;
  r.Free;
  sl.Free;

--------------[ output ]-------------------
match
failed
failed
failed
failed

Based on the syntax documentation from TRegEx (which I believe 'regex' unit closely represents) and looking at the syntax comments inside the regex.pp unit, it should match the following...

Examples:
foob.*r matchs strings like 'foobar', 'foobalkjdflkj9r' and 'foobr'

http://regexpstudio.com/TRegExpr/Help/regexp_syntax.html About halfway down the page...

even trying a filter as follows, match NO items in the stringlist???

  r := TRegexEngine.Create('th');

I expected it to match items '4th' and '5th', but it didn't.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Joost van der Sluis
to Graeme Geldenhuys

Op maandag 01-12-2008 om 13:38 uur [tijdzone +0200], schreef Graeme Geldenhuys:

Show quoted text 22 lines
Hi, I'm using the following regular expression: 'foob.*r' Calling MatchString() against each of the items in the stringlist, I expected it to match 3 items. Instead it only matches the first item and fails all the rest. Why??? --------------[ source code ]------------------- sl := TStringList.Create; sl.Add('1st foobr Item'); sl.Add('2nd Item'); sl.Add('3rd Item'); sl.Add('4th foobaoeuaor Item'); sl.Add('5th foobar Item'); r := TRegexEngine.Create('foob.*r'); x := 0; y := 0; for i := 0 to sl.Count-1 do begin if r.MatchString(sl.Strings[i], x, y) then

Because of the offset y you give it? Try to reset it to 0 within the loop.

Joost

Michalis Kamburelis
to Graeme Geldenhuys: I'm using the following regular expression: 'foob.*r' Calling MatchString() against…
Graeme Geldenhuys wrote 19 lines
Hi, I'm using the following regular expression: 'foob.*r' Calling MatchString() against each of the items in the stringlist, I expected it to match 3 items. Instead it only matches the first item and fails all the rest. Why??? --------------[ source code ]------------------- sl := TStringList.Create; sl.Add('1st foobr Item'); sl.Add('2nd Item'); sl.Add('3rd Item'); sl.Add('4th foobaoeuaor Item'); sl.Add('5th foobar Item'); r := TRegexEngine.Create('foob.*r'); x := 0; y := 0;

You should reset x, y within the loop (before each r.MatchString), not here.

Michalis

Graeme Geldenhuys
to Michalis Kamburelis
Michalis Kamburelis wrote
You should reset x, y within the loop (before each r.MatchString), not here.

Ah, a stupid mistake! Thanks Michalis and Joost. Now my code works like a charm. :-)

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/