I'm Marnanel, and this is my blog. Well, sort of. It isn't timely like a blog, and I don't expect you to subscribe to the feed (though I won't complain if you do). Rather, treat it as a quick trot around my mind, a chat over a pint in the pub, assuming you'd be caught dead talking about software in the pub. You should start here.

Thursday, May 24, 2007

Regular expressions, part 1: why you should care

A magic circle.
It looked an extremely thorough job. Whoever had chalked it was clearly very aware that its purpose was to divide the universe into two bits, the inside and the outside.
, Terry Pratchett
This post is about regular expressions, what they do, and how they can help you.

Everyone loves strings. Most programs spend much of their time shunting them around. They're something that humans love and computers, generally, hate. I mean, they might not tell you to their face, but after hours, when you've all logged out and gone home, let me tell you, your computer is propping up a bar somewhere complaining about you and your human need for string handling.

Humans like strings because they're good for holding human language-- a field which computers, by and large, find opaque and puzzling. I'm not just talking about
Those are language, but human natural language is a vast and difficult and complicated domain which computers hate even more than strings, if that's possible. People have spent their entire lives researching ways to get computers and natural language to play along. So let's set our sights a little lower, at some simpler kinds of communication:
Wouldn't it be nice if there was a simple way of looking at a string and saying what is or isn't an ISBN, or a social security number? If there was a way of dividing the universe into two bits, the SSNs and everything else? Well, that's what regular expressions are for. A regular expression (regex, regexp, or RE to its friends) is a way of dividing all strings into strings which match and strings which don't.

Next part: how to write a regular expression.