Regular Expressions Simple and Powerful.
Yes! Regular Expressions are simple once you learn the grammar, and thats also the biggest problem with them, unless you learn the grammar, Regular Expressions look like the gibberish of some dark art, And unless you actually sit down and study you’ll not make much progress with them.
There are some good books on Regular Expressions.
Regular Expression Pocket Reference
Mastering Regular Expressions
Once you have Regular Expressions in your tool box you’ll quickly see many uses they can be put to such as page scraping or data validation, I’ve even seen them used them for updating Delphi code bases to the latest version of Delphi.
In the train timetable service I used 3 Regular expressions to extract the information need to output optimized version of the time table.
The first two
/<input type=”hidden” name=”DepTime” value=”[0-9][0-9]:[0-9][0-9]
/<nput type=”hidden” name=”ArrTime” value=”[0-9][0-9]:[0-9][0-9]
Are used to strip out the table elements which contain the departure and arrival times,
The strings which match the patterns are stored in two arrays, one for arrival and one of departures.
Then iterating through the two arrays a third regular expression is used
[0-9][0-9]:[0-9][0-9]
This Regular Expression returns the times from the strings contained in the two arrays and it is this information which is used to produce the timetables you see when using the service.
I’d be interested to hear an if there is an even easier way to do this.
There is an excellent tool available for working with Regular Expression, Regex Buddy It is a fantastic piece of software.
Most languages and platforms support Regular Expressions, For Delphi you can use the TRegex component which is free, for Delphi .NET it’s not needed as .NET supports Regular Expressions.