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:
Now, let’s use this function:
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 is my solution for problem mentioned above.
Here’s the code:
* Parse function borrowed from SwissDelphiCenter.
Using the RegExpFindData function, you can neast the expressions in this way: expression1~expression2~expression3 etc.
For example:
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 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 www.protected.co.uk |
One Response
BB King
18|Jan|2008 1Hi…Thanks for the nice read, keep up the interesting posts..what a nice Friday
Leave a reply