with Imagination: by Dustin Diaz

./with Imagination

A JavaScript, CSS, XHTML web log focusing on usability and accessibility by Dustin Diaz

Regular Expressions, the Love-Hate Relationship

Wednesday, November 19th, 2008

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!

15 Responses to “Regular Expressions, the Love-Hate Relationship”

  1. Jim A.

    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.

  2. Wayne P

    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.

  3. George

    I’ve just finished the Friedl book. Totally agree with Jim A. that it is a brilliant piece of work.

  4. Stefan Van Reeth

    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 :)

  5. Nate Klaiber

    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.

  6. Cameron Adams

    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.

  7. Dustin Diaz

    @cameron - hahaha! i can see that.
    @jim - the book is on its way.

  8. Lennie

    @cameron - I suggest Perl or maybe even awk, it’s probably the closest thing to an regular expressions-only language.

  9. Jillian

    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

  10. Colin Morris

    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.

  11. Drew McLellan

    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.

  12. Parenthetical back matching in regular expressions

    [...] me repeat, I still don’t like the taste of vegetables regular expressions. But despite their [...]

  13. Alex Marandon

    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 !-)

  14. Demur Rumed

    @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

  15. diarmuid ryan web design

    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..

Get "JavaScript Design Patterns"

"As a web developer, you'll already know that JavaScript™ is a powerful language, allowing you to add an impressive array of dynamic functionality to otherwise static web sites. But there is more power waiting to be unlocked--JavaScript is capable of full object-oriented capabilities, and by applying OOP principles, best practices, and design patterns to your code, you can make it more powerful, more efficient, and easier to work with alone or as part of a team."

Buy JS Design Patterns from Amazon.com Buy JS Design Patterns from Apress

Submit a Prototype

All content copyright © 2003 - 2007 under the Creative Commons License. Wanna know something? Just ask.

About | Archives | Blog Search

[x] close

Loading...

Submit a prototype

By checking this prototype I agree that I am not submitting false credentials, pornography, or a hate crime website. I also understand that by submitting my entry I may or may not be accepted, and if accepted, my entry may be taken down at any given time if I violate these terms.