Jump to ratings and reviews
Rate this book

Regular Expressions Cookbook

Rate this book
Take the guesswork out of using regular expressions. With more than 140 practical recipes, this cookbook provides everything you need to solve a wide range of real-world problems. Novices will learn basic skills and tools, and programmers and experienced users will find a wealth of detail. Each recipe provides samples you can use right away. This revised edition covers the regular expression flavors used by C#, Java, JavaScript, Perl, PHP, Python, Ruby, and VB.NET. You’ll learn powerful new tricks, avoid flavor-specific gotchas, and save valuable time with this huge library of practical solutions.

493 pages, Paperback

First published May 15, 2009

Loading interface...
Loading interface...

About the author

Jan Goyvaerts

4 books2 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
68 (35%)
4 stars
78 (40%)
3 stars
40 (20%)
2 stars
4 (2%)
1 star
1 (<1%)
Displaying 1 - 11 of 11 reviews
Profile Image for Rob.
Author 2 books410 followers
September 4, 2012
Although I run the risk of fawning all over this book here, Jan Goyvaerts and Steven Levithan's Regular Expressions Cookbook (Second Edition) (O'Reilly, 2012) is a technical text that I will gladly describe using words like "essential" and "indispensable" and "invaluable". It should be on every working programmer's bookshelf, if not on her desk. It is exhaustive and rigorous, covering the major regex flavors across eight popular/widespread general purpose languages. [1] If your work brings you in regular contact with regular expressions, then you need easy access to this book.

To begin with, Goyvaerts and Levithan present an in-depth discussion of each regex feature, starting with the very basics (e.g., making matches against literal expressions) and working up into some pretty sophisticated topics (e.g., writing parsers). True to the title, their approach is a "cookbook" style: a general problem is stated, a solution is presented (or multiple solutions, if that's what it takes), and then they go into an almost painful (but neatly sectioned) level of detail about the solution, describing it token-for-token in some cases. Now, by "neatly sectioned" I mean that their discussion of each solution is broken down by language [2] wherein they are careful to point out flavor- and/or language-specific nuances, quirks, bugs, and/or unique features. They are very careful about this part--if a particular feature does not work in a language (e.g., how JavaScript lacks named capturing groups) then they show you how to work around that deficiency; but perhaps more importantly, if a feature is unique to a language, they point it out as such and caution you against using them (i.e., to keep your regexes general and portable). [3]

Later chapters (i.e., 4 through 9) look at more specific problems--e.g., performing validation on email addresses, [4] dealing with Roman numerals, combing for text in the Apache Common Log Format, or parsing URLs. The recipes are all cross-referenced with each other, so if a particular solution really only solves about 75% of your problem, they're prepared to point you in the right direction. They get right to the point, and then tell you where to go for more. What else can be said about these chapters except that they're like the magnificent arsenal you'll be wishing for when the text zombies swarm at your gate.

All of this makes the Regular Expressions Cookbook very skimmable. It is easy to pick it up, find the particular recipe that is going to help you out of a jam, and power through with that solution in hand. Do you "just" need a quick JavaScript solution? Done. Curious how it might compare to the solution in Java or Ruby? No problem. You skim the surface, or you can go as deep as you need [5] on some very narrow and specific sub-sub-subject within the corpus of regular expressions knowledge. (That being said, take their advice and be sure to read the first three chapters so that you are properly equipped for those deep dives later on.)

As I said before, if your work regularly brings you in contact with regular expressions, you'll want to arm yourself with this. Highly recommended.

---

[1] Goyvaerts and Levithan define the regex flavors as: .NET, Java, JavaScript, PCRE, Perl, Python, and Ruby; the specific languages covered include: C#, Java, JavaScript (and Levithan's XRegExp library), PHP, Perl, Python, Ruby, and VB.NET. They also have a list in chapter 3 of 11 other languages which--while not specifically covered--are applicable because they adhere to one of the flavors.

[2] I should add "where appropriate" here, and note that the per-language sections in each discussion are much more common in the early chapters (2 and 3, with a pretty sharp drop-off starting in 4). This is because they're covering the fundamentals, and there's a lot more in the way of quirks and nuances to tread lightly around at this point.

[3] In other words: they remind you not to get too clever. "Sure you could do that as a one-liner... but no one's going to know what that means next week. Not even you."

[4] Which, validating an email address is not as easy as it sounds.

[5] Or as deep as you want, if you're in to that sort of thing.

---

Full disclosure: I received an electronic copy from the publisher in exchange for writing a review.
12 reviews
August 6, 2017
Contains a clear introduction to what regular expressions are and how to use them. It also covers common pitfalls when working with
regexes like explosive backtracking and the platform specific differences in behavior. For this reason, we are shown the solution expressions for the following languages - C#, Java, JavaScript, PHP, Perl, Python, Ruby, and VB.NET.

Contains (100+) examples for most of the common regular expression use cases like validating email addresses, parsing a Common Log Format file, or extracting a query from a URL. This is a good reference to keep on hand for finding solutions to common problems that can be solved via regular expressions.
12 reviews
January 3, 2019
I read until the end of chapter 5, and skim to the end.
This book is very excellent book to learn regular expression, this book and “mastering regular expression” is must-read regex books.
Profile Image for Jeanne Boyarsky.
Author 32 books73 followers
October 7, 2012
"Regular Expressions Cookbook" covers reg exp syntax in different languages and patterns for building them. I had read O'Reilly's "Mastering Regular Expressions" years ago and liked it a lot. I feel the same way about this book.

While the book doesn't require regular expression experience, it is a tough read if you are coming in completely fresh. At least read a tutorial online first. It is a great book for building on basic or advanced knowledge.

Chapter two is like a book in itself. The author says you can skip this chapter if you've read "Mastering Regular Expressions" cover to cover. That's me! Yet I still learned a couple things in chapter 2.

The rest of the book is recipes. Problem, solution, flavors, languages it works in, variations and lots of description. Some recipes were similar/repetitive. But a 500 page pattern book isn't intended to be read cover to cover. The recipes had a great range from zip codes to ISBN numbers to VAT. I like how the examples build up in complexity so they don't start out being overwhelming.

I learned a lot about regular expressions and patterns. And what grep stands for (g/re/p in ed). I also learned about some new Java 7 features like named captures. I liked the parts on performance and Big O notation.

Now I have two really good regular expression books plus the pocket reference. Thanks O'Reilly!


---
Disclosure: I received a copy of this book from the publisher in exchange for writing this review on behalf of CodeRanch.
Profile Image for Bernie4444.
2,401 reviews11 followers
October 1, 2023
Express yourself!

I have several Regular Expressions books; most of them are from O'Reilly.
However, others are more focused on various UNIX/c environments.

Yet one cannot concentrate on the operating system and ignore what they operate on. This book covers a wider environment that is encountered mostly in websites and occasionally in specific industries (C#, Java, JavaScript, PHP, Perl, Python, Ruby, and VB.NET.) You learn that regular expression even though indispensable is not necessarily uniform in syntax.

You can tell that this was not written in a laboratory or an ivory tower, and the recipes are not just practical but the ones you will be challenged with in the real world.

Do not tell anyone but I have fun reading this book in advance of a chance to see the possibilities for the questions that arise.

While you are being mesmerized by regular expressions you are also being exposed to different environments; some you will be familiar with, and others will make you say where have I been? To name a few various editors, the different languages themselves if you have not had a chance to experiment, each chapter tattle is a different concept or valid reason to use regular expressions.
74 reviews6 followers
June 29, 2015
I read the first couple of chapters, and then skimmed through the rest of the various recipes. It has good coverage of how regular expressions work in general, and the recipes cover a broad range of use cases. It was a good refresher on some areas which I hadn't used much.
316 reviews2 followers
February 29, 2012
Great read! Lots of insightful tips n tidbits!
Definitely a keeper for me!
5 reviews
May 18, 2012
Each and every symbol and constructs making a regular expeession is explained well...you can start feeling confident by reading the first chapter itself..
Strongly recommended
10 reviews
January 2, 2013
A very practical approach, especially the language specific caveats..
Displaying 1 - 11 of 11 reviews

Can't find what you're looking for?

Get help and learn more about the design.