Help with regex lazarus / 31-LCL ---------------------------------------------------------------------- mebus08 · 2016-12-30 10:30 I have a expression like that :
(.*?)<\/div>\((.*?)\) (.*?)<\/span>.*?<\/td><\/tr> to match 1. & 3. 1.
aaa
(bbb) cccxyz 2.
aaa
xxx (xxx) 3.
aaa
(bbb) cccxyz 4.
aaa
(bbb) cccxyz but when i list the matches i become 1. aaa bbb ccc  (ok) 2. aaa
xxx (xxx) (3.bbb) (3.ccc) 3. (4.aaa) (4.bbb) (4.ccc) what is wrong? can you help me please? i used the regexpr unit thanks for help ---------------------------------------------------------------------- Jurassic Pork · 2016-12-30 12:49 hello, can you put your code in the forum ? friendly, J.P ---------------------------------------------------------------------- turronet · 2016-12-30 16:00 Hello, in the second field You are looking for "(" + the content to be extracted + ") " the second string simply doesn't match because there aren't any bracket and space try this regex, it's able to extract all 3 fields Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i
(.*?)<\/div>[\(]*(.*?)[\)]*[ ]*(.*?)<\/span>.*?<\/td><\/tr> Regards Valerio ---------------------------------------------------------------------- Jurassic Pork · 2016-12-30 16:02 with this code : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i implementation uses regexpr;{$R *.lfm}{ TForm1 }procedure TForm1.Button1Click(Sender: TObject);var i : integer;     Re1: TRegExpr;begin      try       re1 := TRegExpr.Create;       re1.Expression := '
(.*?)<\/div>\((.*?)\) (.*?)<\/span>.*?<\/td><\/tr>';       for i := 0 to Memo1.Lines.Count-1 do       begin       if re1.Exec(Memo1.Lines[i]) then  Memo2.Lines.Add( intToStr(i+1) + ' : ' +       Trim(Re1.Match[1]) + ' - ' +       Trim(Re1.Match[2]) + ' - ' +       Trim(Re1.Match[3]));       end;       finally       re1.Free;      end;end; i get this : Quote1 : aaa - bbb - ccc 3 : aaa - bbb - ccc 4 : aaa - bbb - ccc line 2 doesn't match the pattern ---------------------------------------------------------------------- Martin_fr · 2016-12-30 20:22 Assuming that line 2 is not supposed to be matched (it does not have the text between /div and span)... There is a classic mistake QuoteCode: [Select](.*?)<\/div>xxxxx\(This is none greedy. But this does NOT mean "match up to the next "
". It means much up to the next  "
", after which the entire rest of the regex also matches So in line 2 the  "" is not matched by the "\(" part, and therefore the regex searches for another  "". If your text parts to never include any html tags themselves, then match [^<]*? instead of .*? Otherwise (if the regex engine has lookahead syntax) Code: [Select]
((?:[^<]|(?:<(?!\/div)})*?)<\/div>\((.*?)\)and similar for other .*? ((?:[^<]|(?:<(?!div)})*?) explained Quote(              normal ( to catch the matched text   (?:        begin group (do not catch text / not a result group)      [^<]    any none <      |         or     (?:              group        <(?!\/div)    match a <, but only if not followed by /div   (negative look ahead)      }                end group   )*?       end group, and match as often as needed ) this does not work for nested div. ============ if you have no look ahead then make the rest of the reg ex optional, and check in your code if it was present Code: [Select]
(.*?)<\/div>(?:\((.*?)\) (.*?)<\/span>.*?<\/td><\/tr>)? Note that after the /div there is a (?:   )? so the next div can (and will) always be matched, because there is no requirement after that. Again repeat the fix for other .*? ---------------------------------------------------------------------- mebus08 · 2016-12-31 09:48 first thanks for the help the code from  turronet work fine for me. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i
(.*?)<\/div>[\(]*(.*?)[\)]*[ ]*(.*?)<\/span>.*?<\/td><\/tr> when i used the code from Martin_fr i become a exception (TRegEx(comp): Unrecognized Modifier (pos40) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i
(.*?)<\/div>(?:\((.*?)\) (.*?)<\/span>.*?<\/td><\/tr>)? this is the code from me Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var                                                                                                                                                                regex : TRegExpr;begin  regex := tregexpr.Create;  try    regex.Expression:='
(.*?)<\/div>(?:\((.*?)\) (.*?)<\/span>.*?<\/td><\/tr>)?';    if regex.Exec(input) then   ... ---------------------------------------------------------------------- LuizAmérico · 2016-12-31 10:26 No lookahead in TRegExpr: http://bugs.freepascal.org/view.php?id=29756 I use http://regexr.com/ to test the regex before writing the code