a2wd | code & design

Single post

Regular Expressions

The versatile tool for matching patterns

I figured I should know a bit about regular expressions, so for a previous incarnation of the website, I coded the homepage so that it returned the titles of blogs with a link to the respective blog.

In doing so, I found a couple of good resources for regular expressions:

Now using regular expressions to match blog titles is neither the most efficient way nor is it considered best practice but the aim was to learn to use RegExp so I put together the following code which worked on a directory listing of the blog folder and then pulled the titles via a regexp match:

preg_match("/\a2wd \|(.*?)\<\/title\>/",$data,$title);

(this, in PHP).

The main bit here is /\a2wd \|(.*?)\<\/title\>/ which matches everything from the pipe in the title tag to the end of the title tag. It could have been changed to include the first part but that is the same for all pages.

09 Aug 2013code / regexp

  • Recent Posts

  • Archives