My Take on Regular Expressions

We have a new trend where I work – every week or so, we take turns to present a topic to the rest of our peers. It can be on anything cool; by cool I clearly mean technology – all the better if it’s in direct relation to business :D

A workmate suggested I do one on Regular Expressions – and regular expressions it was!

For the uninitiated (and the non-geeks out there), regular expressions – or regex as it’s affectionately known amongst software engineers – allow you to pattern-match strings and manipulate them: you can find, replace, remove, move, etc – as long as you know how to code the right pattern, of course :)

The following is a simple example of Regular Expressions one might write in JavaScript:

/* a simple example */
var str = 'I have mixed feelings for Nyan Cat',
pattern = /mixed/,
out;

out = str.replace(pattern, 'no');

// outputs the string 'I have no feelings for Nyan Cat'
console.log(out);

And now a not-so-simple example:

var str = '<div>8.8.8.8</div>\n'
+ '<span>Leave this!</span>\n'
+ '<address>10.10.0.160</address>\n',
pattern = /^<([^>]+?)>(\d+([.]\d+){3})<\/\1>$/mg,
replacement = '<inet>$2</inet>',
out;

out = str.replace(pattern, replacement);

/*
outputs the string:

'<inet>8.8.8.8</inet>\n<span>Leave this!</span>\n<inet>10.10.0.160</inet>\n'
*/
console.log(out);

The learning curve, steep as it is, provides great benefits – mastery will net you great flexibility and control over any text, of any length.

Here’s a situation to consider – maybe you’re planning on publish a book, and you realized you’ve misspelt a word, i.e. you spelt it ‘dialog’ instead of ‘dialogue’ – you’ll drive the Brits mad! Further inspection into what you’ve written so far yields more problems: sometimes it’s capitalized, sometimes it isn’t. You also noticed parts of your document where the letters in the word ‘dialog’ are spaced apart – ‘D I A L O G’ – and worse, it’s not consistent spacing! If only you knew your way around regular expressions :)

Working on my presentation slides got me thinking … I’ve been using regex for some time, but never have I taken the time to truly understand it inside out – as is the prerequisite with mastery of any skill. Scouring the Internet with my 1337 Googling skills – skills that sink to the level of n00b when my girlfriend “asks”me to find something on the Internet for her – I found loads of sites describing what regular expressions are and how to use them, but the best resource by far is “Mastering Regular Expressions”. I’m not even halfway through, and already my knowledge in regular expressions have far exceeded what it was previously. Kudos to Jeffrey E. F. Friedl for writing this book – In my humble opinion, it’s one of those books every software engineer MUST read!

I’ve embedded the slides (available on slideshare) for your convenience below!



Tags: , ,

Leave a comment

Connect with Facebook