Regular Expressions Cookbook and Mastering Regular Expressions

I have a copy of Mastering Regular Expressions by Jeffrey E. F. Friedl on my bookshelf. I bought it a long time ago to try to improve my skills at using regular expressions to search text and check input against desired norms. While that book is clear and well written, I am sometimes a bit impatient and it was taking too long for me to figure out how to do the things I wanted to do and I got distracted or busy before I read enough to complete the task (I ended up using Google and finding what I needed quickly). I have to admit that I still don’t have the regular expression skills I want to have, although this book promises to teach them to me. Someday it may do so.

When I heard that O’Reilly had an upcoming regular expressions offering to add to their Cookbook series, I was interested, but was finishing up my own title in the series. Once my task was finished, I talked to some friends at the publisher and they kindly sent me a review copy of Regular Expressions Cookbook by Jan Goyvaerts and Steven Levithan. Now that I have had time to read through the book, I can say that it exceeds my expectations and hopes.

What I was looking for was a book that would teach regular expressions while giving concrete examples of real life use cases that I could immediately put to work. This book is filled with them.

Chapters one and two lay the foundation by covering the basics of what regular expressions are, using them to search and replace, match text, and other basic skills. This is good, but where the book really sets itself apart is in chapters three through eight, which are overflowing with useful recipes for things like validating ISBNs, finding URLs within text, stripping leading zeros or matching IP addresses (IPv4 and IPv6). The book has an obvious organization scheme, a ton of useful recipes, and a useful index. Finding what you want or need is very easy to do, and unless your needs are especially unique or esoteric, you will probably discover exactly what you require in the book.

The best part of the book is that every example uses a clear format that sets the stage for an easy discovery of needed information.

First, a problem is stated, such as in chapter four’s item, 4.1 Validate Email Addresses, which says, “You have a form on your website or a dialog box in your application that asks the user for an email address. You want to use a regular expression to validate this email address before trying to send email to it. This reduces the number of emails returned to you as undeliverable.”

Next, a solution is defined, with code examples, accompanied by a description of the particular details that are vital to comprehend when implementing the solution. Next, each recipe has a section for further discussion that leads to a deeper understanding of the regular expression being used and the context in which it is being used.

Especially wonderful is that every recipe has very specific and clear code examples for use with Perl, PCRE (the “Perl Compatible Regular Expressions” library for C, which isn’t identical to Perl’s use of regular expressions, even though it tries), .NET, Java, JavaScript, Python, PHP, and Ruby with notes on which specific release versions or variations of each are covered. When differences exist in the implementation in these environments, those differences are clearly noted and discussed. This feature will make life much easier for people who need to use regular expressions in more than one language context and is a feature of the book I appreciate greatly.

The other regex book on my shelf will remain there until that mystical moment “when I have time to study it.” This book will be used regularly as a reference.

Disclosures: I bought my copy of Mastering Regular Expressions, but was given my copy of Regular Expressions Cookbook free by O’Reilly as a review copy, I also write for O’Reilly and have a book in their Cookbook series.