TRegExpr lazarus / 31-LCL ---------------------------------------------------------------------- jack · 2012-03-24 18:16 I would like to extract portions of a string: Here is an example: I have this string: From this I need 4 Strings: 1) ANAME 2) 20 3) T 4) L for example, in Perl             |^.*<(.*)\(((.*)(.)(.))\).*| would give an array containing these values. Is this available in Lazarus and could someone show me how to code it? Thank you for taking the time to help. ---------------------------------------------------------------------- sfeinst · 2012-03-24 19:05 Wrote a quick hit.  I ran this using your example text.  I had to modify your reg exp to work though.  This is a form with two edit controls.  One for input string one for reg expression.  And  one button. new Reg Exp: ^.*<(.*)\((.*)(.)(.)\).* Also, you need to use the unit SynRegExpr.  To get the unit, I had to add a synedit to my form and then delete it.  If I didn't add the synedit, Lazarus would not find the SynRegExpr unit (probably a path somewhere I did not set up). This is just a quick hit to get you started.  The code should be improved for your needs.  Some things to consider: TRegExp has a Split procedure, but I could not get it to work (in the short time I tried).  Also, though you think it would, I did not find a Count function or property, so I hard-coded the Match[] values.  There should be a better way to do it on the fly.  Here is the code for the button: procedure TForm1.bSplitClick(Sender: TObject); var    RegEx: TRegExpr;   Msg: string; begin   RegEx := TRegExpr.Create;   try     RegEx.Expression := sleReg.Text;     if RegEx.Exec(sleInput.Text) then     begin       ShowMessage('Matches');       Msg := 'Values:';       Msg := Msg + LineEnding + RegEx.Match[0];       Msg := Msg + LineEnding + RegEx.Match[1];       Msg := Msg + LineEnding + RegEx.Match[2];       Msg := Msg + LineEnding + RegEx.Match[3];       Msg := Msg + LineEnding + RegEx.Match[4];       ShowMessage(Msg);     end     else        ShowMessage('No match');   finally     FreeAndNil(RegEx);   end; end; ---------------------------------------------------------------------- ludob · 2012-03-24 19:10 Code: [Select]program Project1; {$mode objfpc}{$H+} uses   regexpr; var   regex:TRegExpr;   i:integer; begin regex:=TRegExpr.Create; try   regex.Expression:='^.*<(.*)\(((.*)(.)(.))\).*';   regex.Exec('');   for i:=1 to regex.SubExprMatchCount do     writeln(regex.Match[i]); finally   regex.Free; end; end. Note that this results in the following output : Code: [Select]ANAME 20TL 20 T L This is because there are too many brackets. Change to Code: [Select]  regex.Expression:='^.*<(.*)\((.*)(.)(.)\).*'; to get Code: [Select]ANAME 20 T L ---------------------------------------------------------------------- ludob · 2012-03-24 19:18 QuoteAlso, you need to use the unit SynRegExpr.  To get the unit, I had to add a synedit to my form and then delete it.  If I didn't add the synedit, Lazarus would not find the SynRegExpr unit (probably a path somewhere I did not set up).As of fpc 2.6.0 regexpr in the fcl is the same code as synregexpr. No need for the LCL anymore ---------------------------------------------------------------------- jack · 2012-03-24 19:39 A thousand thanks this should give me a hook. ---------------------------------------------------------------------- jack · 2012-03-24 19:53 Thanks, Now I see how this works. I had to place the synedit on the form and delete it, there must me something missing in the default setup.  I am glad that you included that trick. Thanks again!! ---------------------------------------------------------------------- sfeinst · 2012-03-25 01:12 Quote from: ludob on March 24, 2012, 08:18:08 pmAs of fpc 2.6.0 regexpr in the fcl is the same code as synregexpr. No need for the LCL anymore Good to know.  I am still running 2.5.1 on my machine.  I'll have to upgrade soon. ---------------------------------------------------------------------- sfeinst · 2012-03-25 01:13 Quote from: ludob on March 24, 2012, 08:10:46 pmregex.SubExprMatchCount That's what I was looking for to put my test in a loop instead of hardcoding the match numbers. ---------------------------------------------------------------------- marcov · 2012-03-25 13:08 Quote from: sfeinst on March 25, 2012, 03:12:01 amQuote from: ludob on March 24, 2012, 08:18:08 pmAs of fpc 2.6.0 regexpr in the fcl is the same code as synregexpr. No need for the LCL anymore Good to know.  I am still running 2.5.1 on my machine.  I'll have to upgrade soon. If it is a 2.5.1 from the second half of 2011 then it will probably also have it. ---------------------------------------------------------------------- tom.chris · 2016-08-12 09:15 PHP to regex anyone? Code: [Select]preg_match("/key: \"(.*?)\",/", $string, $key); ---------------------------------------------------------------------- Thaddy · 2016-08-12 09:56 Quote from: tom.chris on August 12, 2016, 11:15:07 amPHP to regex anyone? Code: [Select]preg_match("/key: \"(.*?)\",/", $string, $key); At least try to use the regexpr and input in the examples. Bit annoying that. But here you go: What about: 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   Writeln(SolveRegExpr('','^.*<(.*)\((.*)(.)(.)\).*'));  Good enough? Outputs: Code: [Select]ANAME,20,T,LHere's the 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 program untitled;uses   classes,RegExpr; function SolveRegexpr(const s,e:string;const d:char = ','):string;var  L:Tstringlist;  RX:TRegExpr;  var i:Integer;begin  RX := TRegExpr.Create;  try    RX.Expression := e;    RX.Exec(s);    L:=TSTringlist.Create;    try    L.StrictDelimiter :=true;      L.Delimiter := d;      for i := 1 to RX.SubExprMatchCount do         L.Add(RX.Match[i]);       Result := L.DelimitedText;    finally      L.Free;     end;  finally    RX.Free;  end;end; begin  Writeln(SolveRegExpr('','^.*<(.*)\((.*)(.)(.)\).*'));end. ---------------------------------------------------------------------- Thaddy · 2016-08-12 10:13 Mmm. I thought so: something like this is already in RexExpr.pas. Nevermind I liked the 5 minutes coding So don't need to react along those lines. I KNOW there is already shorthand like this available in RexExpr.pas I was just annoyed about the PHP remark that missed all points. But it is not exactly like this one and I think it has merit anyway