Regular expressions are strings that are used to match a set of strings, according to certain syntax rules.
You can use them in many places, so it’s worth to learn it. For example, you can use it in Open Office or Total Commander (when searching a file). Regular expressions, called also RegExp, are available in Delphi, but you have to get additional module.
But how to use them in Delphi?

Before reading this post, you should read about Regular Expressions syntax!

There are few Regular Expression modules for Delphi, but most of them are commercial and cost the hell lot of money.
Thanks God, there is one that is freeware -TRegExpr.
Here’s the example of usage:

Code (delphi)
  1. function RegExpFindData(Str, Key : String) : String;
  2. var R : TRegExpr; // declaration
  3. begin
  4.   R:=TRegExpr.Create; // Let’s create the object
  5.   R.Expression:=Key; // Set the expression
  6.   R.Exec(Str); // Execute!
  7.   if R.Exec(Str) then // Is there a match?
  8.      Result:=R.Match[0]; // Jeah! Let’s return it!
  9.   R.Free; // Always remember to free all previously created objects
  10. end;

Now, let’s use this function:

Code (delphi)
  1. RegExpFindData(‘This bread costs $1.25′, ‘\$[0-9]{1,}\.[0-9]{1,}’);

It will return “$1.25″. So simple!

But if you are parsing html pages, sometimes to get the thing you want, you need to create extremely long expressions that will stop working when someone add just one space somewhere in the html. It would be nice to use nested regular expressions, isn’t it?
So first, you’d get some bigger part with data you want inside, next you’d parse the thing you need from the bigger part previously parsed.

NestedRegExp

NestedRegExp is my solution for problem mentioned above.
Here’s the code:

Code (delphi)
  1. function Parse(Char, S: string; Count: Integer): string;
  2. var
  3.   I: Integer;
  4.   T,STR: string;
  5. begin
  6.   try
  7.     str:=s;
  8.     if Char=‘ ‘ then while pos(‘  ‘, str)<>0 do
  9.       str:=StringReplace(Str, ‘  ‘, ‘ ‘, [rfReplaceAll]);
  10.     if (Length(Str)>0) and (Str[Length(Str)] <> Char) then
  11.       Str := Str + Char;
  12.     for I := 1 to Count do
  13.     begin
  14.       T := Copy(Str, 0, Pos(Char, Str) - 1);
  15.       Str := Copy(Str, Pos(Char, Str) + 1, Length(Str));
  16.     end;
  17.     Result := T;
  18.   except end
  19. end;
  20.  
  21. function RegExpFindData(Str, Key : String) : String;
  22. var R : TRegExpr;
  23. begin
  24.   if (Key=) or (Str=) then Exit;
  25.   R:=TRegExpr.Create;
  26.   if pos(‘~’, Key)<>0 then R.Expression:=Parse(‘~’, Key, 1) else
  27.                            R.Expression:=Key;
  28.   if R.Exec(Str) then
  29.      Result:=R.Match[0];
  30.   R.Free;
  31.   if pos(‘~’, Key)<>0 then
  32.     Result:=RegExpFindData(Result, Copy(Key, pos(‘~’, Key)+1, Length(Key)-pos(‘~’, Key)));
  33. end;

* Parse function borrowed from SwissDelphiCenter.

Using the RegExpFindData function, you can neast the expressions in this way: expression1~expression2~expression3 etc.
For example:

Code (delphi)
  1. RegExpFindData(’sfgfgfdg begin [datawewant] end fslkjflgj’, ‘begin.*end~\[.*\]~[a-z]{1,}’);

So, first the function will execute “begin.*end” expression for “sfgfgfdg begin [datawewant] end fslkjflgj”.
By doing this, it will get “begin [datawewant] end”.
Next it will run second expression - “\[.*\]” on the “begin [datawewant] end”, this will return “[datawewant]”.
Finally, after running “[a-z]{1,}” expression on “[datawewant]”, it will give us the “datawewant”.
Trust me, it makes live a lot simpler.

Regular expressions make parsing web pages really easy and changeproof.
When I am parsing something, I always use it.
You should also try this!

Download

Download TRegExpr 0.952 + help

Also, don’t forget to visit TRegExpr homepage - you can read there much more about RegExp and it’s syntax.

Compare Life Insurance

We search 300 life insurance plans
Get our cheapest quote online now!

www.protected.co.uk

Matched.co.uk