[SOLVED] Regex does not replace lazarus / 21-General ---------------------------------------------------------------------- DanishMale · 2024-11-20 09:23 Hi I have written this where I need to "convert" HTML to CSV: 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              const                  RegexprExpression: String = '/(
)([0- 9]{1,2})(.+)(artistnavn">)(.+)(<\/div>
)(.+)(<\/div>
)(.+)/'; .......              BaseData := '
24
   
       
   
3
27
4
37
5
1
KIM LARSEN
MINE DAMER OG HERRER
EMI DANMARK
                                     
2
3xPLATIN
                   
1
7
                   
                                                                                                                                     
       
'; procedure .....               Y := 2011;                W := 2;                Media := 'A';                 ReplaceText:= '"$7","DK","$9","'+IntToStr(Y)+'"," '+IntToStr(W)+'","$4","'+Media+'"';                MyRegex2 := TRegexpr.Create;                MyRegex2.ModifierG := False;              MyRegex2.ModifierI := True;                MyRegex2.Expression := RegexprExpression;                BaseData := Trim(BaseData);              ResultText:= MyRegex2.Replace(BaseData ,ReplaceText, True);              MyRegex2.Free;                  ConvertMemo.Lines.Add(ResultText); // Expected result or similar : "Kim Larsen","DK","Mine Damer og Herrer","2011","2","30","A" // Real result :
24
 
       
   
3
27
4
37
5
1
KIM LARSEN
MINE DAMER OG HERRER
EMI DANMARK
                                     
2
3xPLATIN
                   
1
7
                   
                                                                                                                                     
       
   It works when I test on f.ex regex101.com .... What have I missed ? ---------------------------------------------------------------------- Martin_fr · 2024-11-20 09:34 Are the "/" at the start and end expected? Your base data does not contain any "/div id=". If you copied them from some script, then they are probably wrong. In various script languages the / serves as enclosing token (quotation mark) for the regex. But here you have the regex as string, it is enclosed by the single quotes '. ---------------------------------------------------------------------- DanishMale · 2024-11-20 09:49 Ahh got it .. thanks for your hint, because on another RegexPr in the samme app I need / in the expression, However it isn't needed in this one. ---------------------------------------------------------------------- MarkMLl · 2024-11-20 21:06 Quote from: DanishMale on November 20, 2024, 10:49:02 amAhh got it .. thanks for your hint, because on another RegexPr in the samme app I need / in the expression, However it isn't needed in this one. Note that the leading and trailing slash are conventions from- in particular- Perl-Compatible Regular Expressions (PCREs), which you might have come across in various cookbooks. However at this point I also have to cite https://stackoverflow.com/questions/1732348/regex-match-open-tags- except-xhtml-self-contained-tags The point- as understood by this non-CS grad- is that parsing HTML requires various back-references etc. (e.g. "I'm closing a tag but what opened it?") which aren't covered by the original definition of Regular Expression. MarkMLl ---------------------------------------------------------------------- Jurassic Pork · 2024-11-21 00:31 Hello, you can also use  HTML parser  example : 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 program project1;uses  classes,sax_html,dom_html,dom;const  testdata = '
'           + 'this is just a simple example'         + '
'           ;var  doc: thtmldocument;  els: tdomnodelist;begin  readhtmlfile(doc,tstringstream.create(testdata));  els := doc.GetElementsByTagName('div');  if els.Count > 0 then begin  writeln(tdomelement(els[0]).getattribute('id'));    writeln(tdomelement(els[0]).getattribute('multiple'));    writeln(tdomelement(els[0]).textcontent);  end;  readln;end.        but your data seems not to be a valid HTML : Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    
   
24
   
       
               
       
       
           
3
27
       
           
4
37
       
           
5
1
   
   
       
   
       
KIM LARSEN
       
MINE DAMER OG HERRER
       
            EMI DANMARK   
   
2
 
3xPLATIN
   
1
   
7
                                                               
it seems that GetElementById doesn't work for thtmldocument. You can try to use xpath (not working for all HTML). Example of use : 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 program project1;uses  classes,sax_html,dom_html,dom,xpath;const  BaseData =  '' +              '
' +                   '
24
'  +              '
'  +              '
' +                 '
' +                   '
KIM LARSEN
'  +                   '
MINE DAMER OG HERRER
' +                     '
' +                        'EMI DANMARK' +                     '
' +                 '
'  +             '
'  +           '
' +                 '
2
' +                 '
3xPLATIN
' +                 '
1
' +                 '
7
' +             '
' +             '';var  doc: tHtmldocument;  els: tdomnodelist;  elm: tdomelement;  XPathResult: TXPathVariable;begin  readHtmlfile(doc,tstringstream.create(BaseData));  XPathResult := Eval uateXPathExpression('//div[@id="artistnavn"]',doc.DocumentElement);  writeln(String(XPathResult.AsText));  XPathResult := EvaluateXPathExpression('//div[@id="titel"]',doc.DocumentElement);  writeln(String(XPathResult.AsText));  XPathResult := EvaluateXPathExpression('//div[@id="denneuge"]',doc.DocumentElement);  writeln(String(XPathResult.AsText));//  elm := doc.GetElementById('artistnavn');//  writeln('artistnavn: ' +  tdomelement(doc.GetElementById('artistnavn')).TextContent);//  writeln('title: ' +  tdomelement(doc.GetElementById('title')).TextContent);//  writeln('denneuge: ' +  tdomelement(doc.GetElementById('denneuge')).TextContent);  readln;end.   Friendly, J.P