Regular Expressions Simple and Powerful.

Eoin | Delphi, IT, Internet, PHP, REST, Software, WebServices, mobile | Thursday, May 24th, 2007

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.

The RESTful URL

Eoin | IT, Internet, PHP, REST, Uncategorized, WebServices | Tuesday, May 22nd, 2007

When creating the train time table service I decided to use a RESTful approach for the API, This meant the URL should confirm to a number of RESTful Principles.

1) The URL represents a resource and consist of NOUNs, not VERBs.
If you using Verbs then you are not using REST. you’re using RPC.
One of the defining principles of REST is the use of a limited set of Verbs POST,GET,PUT and DELETE which correspond to Create, Read, Update and Delete (CRUD).
This allows the easy contruction of URLS so that a consumer and go directly to the resource required.
for example
http://www.eoinprout.com/trains/Cork/Cobh/
will return the Cork Cobh time table resource.

2) The URL hides implementation details.
There are many advantages to hiding the implementation details not the least of which is that it allows the background implementation to be changed without affecting the API.
In my case the service is implemented in PHP but by using mod_rewrite it was possible to hide the implementation details of the service, One more advantage of this is that many search engines do not properly index dynamic pages which require the correct parameters to be set. This aids the discovery of the service.

3) The service should expose other parts of the service via links allowing discovery and traversal.
The trick here is not expose everything in one URL. In the case of my service the user starts at ../trains/ which presents a list of departure stations and links to a list of possible destinations, from there another link will return the URL for the particular time table required. so all the users needs to remeber is the ../trains/ URL.

4) GETs are used for obtaining a copy of the resource.
GET is the standard HTTP method getting content when you type a URL into the address bar of the browser it is a GET which is executed , On IrishRail.Com POSTs are used to retrieve a copy of the time table a user may be looking for.
There are serious disadvantages when using POSTs to retrieve a resource, The resource cannot be bookmarked, The URL for the resource cannot be sent to someone else, URL cannot be constructed and search engines cannot index the resource.

Irish Rail Train Timetables Service Optimised For Mobile Internet

Eoin | Internet, PHP, REST, Uncategorized, WebServices, mobile | Sunday, May 20th, 2007

In one of my previous posts I moaned about the high cost of the mobile internet in Ireland, The site I used as an example was Irishrail.com, I use this site a lot, checking the Cork/Cobh timetables. I wanted to be able to check the train times on my mobile but I wouldn’t use the Irish Rail website when it costs so much to do so. I decided to create a REST style web service optimized for the mobile internet, i.e. use as little bandwidth as possible.
It’s also be a chance to try out some ideas related to REST.

IMHO REST is the way to construct web services.

The Service is available here and you get your timetable by traversing the links.
OR
You can go directly to the timetable you want by constructing the URL based in the Format
http://www.eoinprout.com/trains/DeparturePoint/Destination
For Example
http://www.eoinprout.com/Cobh/Cork/
http://www.eoinprout.com/Cork/Cobh/

The service will show the timetable for trains between your destination and point of departure for the day, bookmarking the timetable will allow you to quickly check “Todays” timetable.
The service returns “Todays” timetables, If I’m planning journeys far in advance I’ll use my PC, The phone is for when I need to check when the next train home is :). If anyone really wants it changed to support dates in the future just ask.

Of course the first question you ask is, “How much bandwidth does it save ?
It saves a lot.

Irishrail.com eoinprout.com/trains/
Traversing Links 167kb 9kb
Constructing Link Not possible 1kb



It’s not possible to construct the URL for a particular timetable on Irishrail.com because it’s using a POST to send parameters rather then a GET, Interestingly this means that the timetable cannot be bookmarked to allow quick access.

The service was created using PHP and mod_rewrite, which I’ll talk more about in the future as I’m going to be using this service to illustrate REST.

Using PHPEdit With PHP4

Eoin | IT, PHP | Monday, April 16th, 2007

PHPEdit is a great tool but by default PHPEdit (In this case ver2.10) installs with PHP5, and if like me, you need to develop with PHP4 then you’ll need to change the version of PHP PHEdit uses to debug.

Assuming that you’ve just installed PHPEdit then,

1) Get the required version of PHP, PHP 4.4.6

2) Extract the contents of the download to a permanent folder e.g. “PHP4Folder”, There is no need to do any sort of installation beyond this.

3) You will need to get the XDebug module if it is not already in the PHP4Folder/extensions folder and add it there.

4) Rename php.ini-dist to php.ini and open it in notepad.

Add the following lines to the php.ini file
zend_extension_ts=”./extensions/php_xdebug.dll”
xdebug.remote_enable=on
xdebug.remote_autostart=on
xdebug.profiler_enable_trigger=on
xdebug.profiler_enable=on
xdebug.profiler_output_dir=”./profiler”
xdebug.profiler_output_name=”timestamp”p”

and save it.

6) Open PHPEdit and go to the Debugger Settings,

Change “CGI php executble” to point at php.exe in the “PHP4Folder”.

restart PHPEdit and your all set to go.

7) To make sure that your using the correct version of PHP create a small script calling phpinfo(), looking at the output will allow you to confirm which version of PHP PHPEdit is using.

Powered by WordPress | Theme by Roy Tanck