Regular Expressions, the Love-Hate Relationship
There are few things in life that I outright hate, but always come back to; and vegetables come close. Without seasonings, oils, and creams, I can easily excuse this food group. What I’m actually talking about here is regular expressions. Similar to vegetables, they can be yucky but good for you. And no matter how old you grow to be, they’re always there taunting you. There is absolutely no way you can live as a programmer and not have to swallow a few.
Nevertheless, just the other day I found my problem with Regular Expressions. My process is wrong. Similar to eating vegetables, I try and skip the chewing, and simply eat them whole. With vegetables, this is a little bit easier. But with regular expressions, it prolongs the pain. Let’s take for example some of my most favorite foods I enjoy like dessert wine, and Filet Mignon. Each having tastes to savor and slowly enjoy, putting a cheery happy smile on my face. Talk to anyone who is good at regular expressions, and you’ll notice they often enjoy them just a little more than your average person.
Carefully note the differences between enjoying your food slowly versus trying to swallow it whole. The same goes for regular expressions. Cut them up into pieces, and take small bites. Match smaller portions, the rest isn’t going to leave your plate any time soon.
Why do I say all this?
I mean, aren’t all Google employees required to how to crack a CIA database? Kidding aside, even to this day I have continued to avoid using regular expressions when I know how useful they could be. So therefore I am now saying all this today to announce my early 2009 programmers resolution to eat more regular expressions. Upon this declaration I will be reading the highly recommended Mastering Regular Expressions by Jeffrey Friedl.
Also, where have I been?
Good question. I’ve been working on Gmail since last February building high fidelity prototypes to test new ideas. I’ve also been taking several photos and getting much heavier into photography than ever before. There is still rarely a day that goes by where my camera isn’t by my side. Feel free to have a gander at my featured photos. Other than that, Happy Upcoming Turkey Day. Cheers!




November 19th, 2008 at 11:16 am
I have a reputation at my workplace for loving the two parts of web development that many others seem to just hate–JavaScript and Regular Expressions. I know you share my love for JS–I hope you can come over to the RE side as well!
The book you mention is THE book to read for RegEx. And he says what you said–go char by char in your expression.
Glad to see you posting again and wow–you’re a GMail dev. Just wow.
November 19th, 2008 at 12:42 pm
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems. –”Jamie Zawinski” :)
I’ve been known to over use regexs but once you start thinking in regexs… it’s hard to stop even though a simple non re .replace does the job.
November 20th, 2008 at 1:06 am
I’ve just finished the Friedl book. Totally agree with Jim A. that it is a brilliant piece of work.
November 20th, 2008 at 4:26 am
Good to know I’m not the only one building RegExps in baby steps. Always worked for me, but one can hardly call this practice ‘professional’. After years of writing them I still use reference material when building them up. Also not very ‘professional’ indeed, but it works and in the end that’s all that counts :)
November 20th, 2008 at 6:04 am
I am with Wayne P on this one. I think there are times when I may over-use them in my apps. One of the first things I do when setting up an app (in Rails) is create a format.rb file, that collectively builds on all different types of regular expressions for different formatting needs (it evolves as I find better ways to validate some of the tougher items). Makes my life easier when validating, or even parsing, all of the different formats I run into.
November 20th, 2008 at 7:16 am
I loooooooooove regular expressions. I’m trying to find a programming language that involves nothing *but* regular expressions. One giant, nested regular expression with all the extra add-on bits that make them so delicious.
November 20th, 2008 at 2:11 pm
@cameron - hahaha! i can see that.
@jim - the book is on its way.
November 22nd, 2008 at 7:30 am
@cameron - I suggest Perl or maybe even awk, it’s probably the closest thing to an regular expressions-only language.
November 22nd, 2008 at 10:59 pm
Usually I just read your posts, but the mama in me has to say that if you don’t like vegetables then perhaps they haven’t been cooked right, or perhaps they weren’t introduced to you as a youngster the right way. So for those of us with this strong mothering instinct, try to keep an open mind about veggies, OK?
But seriously, thanks for the link to the book by Jeffrey Friedl. I’ll give it a read.
Jillian
November 23rd, 2008 at 3:41 pm
I’m another of the coders who love regular expressions. Even if their implementations are occasionally java-esque (write once, debug everywhere) thanks to the different language implementations of how they work. Evil.
Look up XKCD’s regular expression comic (and buy the shirt, I did ;)) and you’ll see a good way to come over to the regex side.
November 24th, 2008 at 3:16 am
A wise old friend once told me “learn regular expressions and impress your friends!”. I did, and it did, and I’ve not looked back.
Programming tasks aside, just the usefulness of regexp within the Find & Replace in a text editor makes them worth the pain alone. And the pain, really, isn’t all the great at all.
November 27th, 2008 at 6:02 pm
[...] me repeat, I still don’t like the taste of vegetables regular expressions. But despite their [...]
November 29th, 2008 at 8:46 pm
Mastering Regular Expressions is the first programming book I’ve ever read. I was learning Perl as my first programming language and regular expressions seem to be so mysterious that I thought reading that book would help. I’m glad I did because since I’ve always enjoyed using regular expressions.
Sometimes I come across code in which I can tell the author is not familiar with regular expression and uses complex string manipulations to do what a simple regexp could have done.
As for not liking vegetables, this is just plain science fiction to me !-)
December 21st, 2008 at 10:20 pm
@Cameron Adams: Look no further, http://serprex.staticfire.co.uk/lang/rue.htm
Yes, when I first ran into these odd things I was frightened. Then I actually sat down yesterday and read the pydocs on them to find that they seemed quite useful. I’m already feeling that urge to replace perfectly simple string handling code with regex
January 12th, 2009 at 6:39 am
regular expressions are one of the fundamental staples of coding in my opinion, especially when it comes to data munging and text manipulation…a simple well written one line regex can save dozens of lines of convoluted loops and if statements.
it is true they are unsightly and scary at first but anything truly worthwhile is never easy in this life..