Coding Horror

programming and human factors

Regular Expressions for Regular Programmers

If you've followed my blog for any length of time, you know that I am a total regular expression fanboy. It's almost embarrassing how much I love the damn things. I'm pretty sure my teammates roll their eyes every time they see yet another class I've touched that has using System.Text.RegularExpressions at the top. You might as well rename it to JeffHasBeenHere.

I say that because I end up writing a lot of string handling code, even when people tell me I shouldn't. Now, I only advocate responsible and judicious use of regular expressions when you happen to be dealing with strings. In the wrong hands, regular expressions can be dangerous. You might end up wondering if Q*Bert just vomited all over your source code. Or you might be programming in Perl. Is there any difference? (instant rimshot)

But I digress. Although I love regex, I've never been a fan of the classic regular expression reference book, Friedl's Mastering Regular Expressions. I found it dry, a bit academic, and lacking in practical real world examples. It just didn't speak to me as a working programmer in the way that regular expressions themselves did, and that was disappointing.

That's why I was so excited to discover that two of the gnarliest regex gurus I knew – Jan Goyvaerts (author of RegexBuddy and regular-expressions.info) and Steven Levithan (author of XRegExp and RegexPal) – were putting their heads together to create a regular expression reference for the rest of us. I immediately pre-ordered it sight unseen.

That book is Regular Expressions Cookbook. It arrived a few days ago, and although my expectations were high, I think this book has exceeded even the loftiest expectations I had. It is outstanding.

regular-expressions-cookbook.png

What I love about this book is twothree things:

  1. It's filled with practical, real world examples of RegEx use. At every step of the way, from beginner to master level, you're building regular expressions that are actually useful in the wild, and not just abstract, obtuse academic exercises in solving string matching puzzles.

  2. It covers all the common gotchas that you inevitably run into when you start building non-trivial regular expressions. Things like the sometimes massive (and painful) differences between regex libraries in various languages, subtle regex flavor quirks, catastrophic backtracking, unicode support, and so forth. These are all presented in context of the solutions, exactly as you'd encounter them in real programming. I know because I have the scars to prove it.

  3. It was updated and revised in the second edition to reflect current flavor differences with new recipes, plus a whole new chapter on source code and log files. It also covers XRegExp, an emerging standard, fully-featured regex library for JavaScript.

Regular Expressions Cookbook manages to be simultaneously accessible and almost ridiculously comprehensive. I consider myself a fairly advanced regex user and about 50 pages in I've already had three big "oh, wow, I didn't realize that" moments. In my mind, at least, this completely replaces the Friedl book as the go-to reference for programmers of any skill level or background who seek regular expression enlightenment.

Needless to say, recommended.

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: https://infosec.exchange/@codinghorror