Skip to main content
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Mark Drew
Ben Nadel at CFCamp 2023 (Freising, Germany) with: Mark Drew ( @markdrew )

The Regular Expression Cookbook By Steven Levithan And Jan Goyvaerts

By on

A few weeks ago, I was super excited to learn that regex ninja Steven Levithan coauthored the new O'Reilly book, Regular Expression Cookbook with Jan Goyvaerts. If the name Steve Levithan sounds familiar it's because Steve is a long time participant on this blog, often swooping in to demonstrate his crazy regular expression skills, pointing out issues, offering efficiencies, and leaving very detailed explanations of how regular expressions actually work; from catastrophic backtracking to "unrolling the loop," Steve has cemented himself as the go-to guy for regular expression goodness.


 
 
 

 
The Regular Expression Cookbook By Steven Levithan And Jan Goyvaerts, Released By O'Reilly Books.  
 
 
 

Over the weekend, I was able to carve out time to read the Regular Expression Cookbook and I can tell you that it is an awesome book. Not only does it contain a huge number of "recipes" for using regular expressions to solve real world problems, it has a thorough and easy-to-follow tutorial on what regular expressions are and how they work. Even if you are someone who feels very comfortable with regular expressions, I would highly recommend reading the tutorial - you will definitely learn things that you didn't know before. I certainly found myself dog-earing some new tip every few pages.

Now, not only are there some great examples in this book, the examples are executed and explained in eight different programming languages. Of course, you don't need to read them all if they are not relevant; but, I found the Java and the JavaScript explanations quite enlightening as I deal with these languages every day. For example, did you know that using the exec() command could cause infinite loops in FireFox? Did you know that you could reset the pattern Matcher object in Java if you wanted to reuse it with another subject? Do you know what the String::replaceAll() method is actually doing behind the scenes?

After the first few hours of reading, I'm sure there is only so much that my brain could retain. Of course, that's the beauty of the "Cookbook" - when you need to look something up, you have it at your finger tips. But, even if there are things that I can't remember today, just seeing the way Steve uses regular expressions to solve problems is quite inspiring. His use of lookarounds to validate inputs was brilliant. His explanations of atomic grouping and possessive qualifiers finally made it make sense to me. Even beyond the everyday problems that he solves, his examples and explanations gave me a better handle on regular expressions as a whole.

Who is this book meant for? Anyone that wants to learn more about regular expressions and how they can make your life easier. For those just getting into regex, it's easy enough to follow; and for those that have years of experience, it's thorough enough to teach you a number of tricks. I highly recommend this book for anyone who wants to get into regular expressions for the first time or for anyone who wants to take their existing regular expression skills to the next level.

Reader Comments

92 Comments

Awesome Ben! I LOVE regular expressions. They make even basic text editing quicker, and more painless. I use them to quickly parse through large amounts of text, code, or the like.

One of my personal favorites:

[ \t]+$

I hate having tabs or spaces on blank lines, or at the end of lines. This little gem removes any number of spaces/tabs at the end of a line.

6 Comments

I have a copy of this book and, not that anyone here needs another recommendation after Ben's, but it's fantastic!

I have long sucked at Regular Expressions and avoided them at all costs. But being able to use this book as a reference to see how so many RegExp practices and 'tricks' are done has made me better and more comfortable using them in my apps. The authors did an awesome job.

34 Comments

Have you read Jeffrey Friedl's Mastering Regular Expressions?? If so, is it better than that? Because from what I've read and heard, that book is the daddy of all books when it comes to Regular Expressions.

29 Comments

Thanks for the recommendation. Deffo gonna give it a shot! I'm in love with regular expressions since 2005 when I stumbled on the co-author's regular expressions info site and subsequently built a code (c++) highlighting integration for phpbb.

1 Comments

I found learning regular expressions pretty tough. I usually find the resources I need online, but with differences in the regex engine between languages, I had a hard time finding a good tutorial. One thing I did find that makes regex so much easier is the application Expresso. It is free and it makes writing and learning regex a breeze. Some features: translates your regex into understandable english. you can run (partial)matches with your regex against data you give. also has an expression builder where instead of you writing the expression, you can tell the application what you want the exp to do piece by piece and it will create the expression for you. Mostly I use the program to make sure I have the expression right without having to make testing pages just to see if my regex works. The program is located at http://www.ultrapico.com/. Great article I will definitely check out the book!

15,640 Comments

@Samuel,

I use an app called "The RegEx Coach". I am pretty sure Steven uses one called RegEx Buddy, which, from the book's description, looks like a very beasty application.

22 Comments

I used to consider all regex to be "black magic". My boss would say "I made a cool regex to do this or that" and I would ward him off with the sign of the cross and do a ritual cleansing.

Lately, however, I have found myself using regex more and more. (A lot thanks to PowerGREP, what a great program that thing is!) Clearly there are spells that take many moons to learn, but you can accomplish quite a bit with them if you take the time to piece together what you need. (And hope the language differences aren't that impactful to your goal.)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel