Oakland.pm

Reviews

Review of "Mastering Regular Expressions"

author: Jeffrey Friedl

reviewer: George Woolley


Short Review and Recommendation

Mastering Regular Expressions, 2nd Edition
By Jeffrey E. F. Friedl
2nd Edition July 2002
0-596-00289-0, Order Number: 2890
484 pages, $39.95 US, $61.95 CA, £28.50 UK

Notes:

  • Above is basic catalog information for the book.
  • Click on the cover image above to go to the full catalog entry.

Exceptional   :) :) :) :) :) of 5

If regular expressions are important to you, get this book.

"Mastering Regular Expressions" is exceptional both in the scope of what it covers regarding regular expressions and also in it's thoroughness. To date this is the most impressive technical book I've read on any subject. It's a virtuoso performance.

One caveat: parts of the book are quite demanding.

See my longer review for more detail.

George Woolley of Oakland.pm

Miscellaneous

Chapter Titles

  • 1. Introduction to Regex
  • 2. Extended Introductory Examples
  • 3. Overview of Regex Features and Flavors
  • 4. The Mechanics of Exp Processing
  • 5. Practical Regex Techniques
  • 6. Crafting an Efficient Exp
  • 7. Perl
  • 8. Java
  • 9. .NET

Notes:

  • "Regular Expression" is abbreviated to "Regex" above.
  • "Expression" by itself is abbreviated "Exp".

Fun Quote

"with Perl, you become skilled by repeatedly shooting yourself in the foot."

Notes:

  • This quote is from chapter 7, the chapter on Perl.
  • It is presented here out of context because it's more fun that way.

A Related Reference

Notes:

  • The "Regular Expression Pocket Reference" can be thought of as summarizing features and syntax described in "Mastering Regular Expressions".
  • Click on the image above, to go to the catalog entry for this reference.
  • Or if you wish, check out my review of this reference.

A Related Article

Five Habits for Successful Regular Expressions

Notes:

  • In his article "Five Habits for Successful Regular Expressions" Tony Stubblebine talks about (duh) five habits that will help you write regular expressions that work. One of the habits has to do with testing.
  • Click on the image above, to go to the article.
  • Or if you wish, check out my review of this article.

Longer Review

Contents

Note:

  • "Teaching Regex" might more accurately be "conveying insights about Regex".

Note:

The Title

the meaning of the title: The book "Mastering Regular Expressions" doesn't belong to an O'Reilly series. So I don't have that to guide me in interpreting the title. However, the author is quite helpful regarding this.

According to the author the book provides the information needed to acquire a "full command" (that's roughly synonymous with mastery to me) of regular expressions. It is the author's intent that the book also provide the motivation to use the information to acquire that "full command".

I gather that for the author "information" includes:

  • lists of regex features in particular applications
  • explanations of how they work and why
  • hints, tips, pitfalls

but also

  • useful ways of thinking about things
  • useful habits to have

does the author deliver? I don't know whether the book contains enough information to master regular expressions. If I ever feel like I'm in "full command" of regular expressions, perhaps I'll have an opinion. What I can say is that I know of no other book on regular expressions with comparable scope and detail.

Does the author provide the motivation to use this information? Again, I can't say. What I can say is that the author sure motivated me. The book was quite difficult for me at times, but I've now been through all of it. Some of the things contributing to my motivation have been:

  • the title of the book
  • the broad scope of the book
  • the great detail of the book
  • the author's guidance when things were more difficult
  • the reputation of the author

About the Reviewer

Biases: Some of my biases that could effect this review are

  • I'm big into string manipulation.
  • Perl is my favorite language.
  • I'm a fan of O'Reilly.

If you want more detail, take a look at the "About the Reviewer" section of my review of the article "Five Habits for Successful Regular Expressions" by Tony Stubblebine.

Oh, I own the first edition of the book being reviewed here and consider it excellent. I thought it was so good, that I bought a second copy so I could have one copy at work and one copy at home.

Expectations: What I was hoping for in this book was

  • to improve my ability to write regular expressions
  • to improve my ability to teach regular expressions

Relation to First Edition

The first edition came out in 1997, the second in 2002. That's a period of five years. There were many advances in regexes during that period. Perl regexes evolved. Java, which didn't have regexes at all in 1997, got them in spades in 2002. Many languages that had regexes upgraded them during that period. And some new languages (like Ruby), that came into being during that period, support regexes.

If you have the first edition, you'll find much that's familiar in the first seven chapters. You'll also encounter many changes and additions to reflect the changes that have taken place. And chapters 8 and 9 on Java and .NET regexes are entirely new.

The following table gives a crude picture of the relationship between the chapters of the first and second editions.

2nd Edition ChapterCorresponding
First Edition
Chapter
1. Intro to Regex1
2. Extended Intro Examples2
3. Overview of Features/Flavors3
4. Mechanics of Exp Processing4a
5. Practical Regex Techniques4b
6. Crafting an Efficient Exp5
7. Perl7
8. Javanone
9. .NETnone

Notes:

  • Words in the chapter names are abbreviated in some cases.
  • There is nothing in the second edition corresponding to chapter 6 of the first edition.

What I Learned about Writing Regex

Well, I learned a great deal. Even before this review was complete, I began using the following in some of my Perl code:

  • embedded code in regexes to assist me in understanding how they work.
  • the qr operator to help me more easily build up complex regular expressions from simpler ones

Before reading this book, I was unaware that I could embed code in regexes and I was only vaguely aware of the qr operator. Both are explained in a clear way in the book and there are examples.

my point: Well, for all I know, you already use emedded code and qr. Still, I suggest taking a look at this book.

Or maybe Perl isn't the world you do regexes in. Well, the book also covers Java and .NET and much more. And it becomes clear reading the book that as far as regex features are concerned other languages are competitive with Perl. Perl has a prominent place in this book, but this is definitely not a Perl book.

Well, I learned a lot, and I am already putting some of what I learned to use.

What I Learned about Teaching Regex

From time to time, I get an opportunity to help a novice Perl programmer become more proficient at Perl programming in general and using regexes in particular. Reading "Mastering Regular Expressions" has put me in a better position for doing that. Below I briefly talk about two ways I expect the book to help me in this regard.

The Language Analogy: The language analogy Friedl puts forward in the book has been helpful to me, and I believe it would also be useful to novices.

Here's the picture I have from the language analogy. Perl is a programming language which includes regular expressions which are a language which includes character classes which are a mini language.

It's important to recognize that these three languages are (beautifully) integrated together in Perl. It's also often useful to recognize them as three languages. In this context, here's something Friedl says about character classes that I've found useful:

"Consider character classes as their own mini language. The rules regarding which metacharacters are supported (and what they do) are completely different inside and outside of character classes."

And the author gives concrete examples of this.

"Mastering Regular Expressions" as a reference: I have become convinced that this book could be useful for a Perl novice who:

  • has been using regular expressions for a while
  • is serious about gaining a deeper understanding of them

For such a person, I'd suggest the following use of the book:

chapter(s)suggestion
prefaceread thoroughly
chapter 1read thoroughly
chapter 2read thoroughly
chapters 3&7scan
and read parts of interest
chapters 4-6, 8-9just be generally aware
of contents

my point: My point is that this book can be useful to you in helping novices. The above are just two of a number of things I expect to be useful in teaching the regex novices I encounter who, it happens, are also typically learning Perl. Hey, you may live in a Java-centric world. What I'm saying is that there is valuable material here for a novice and for people who are oriented to helping novices.

I'm thinking this might be missed by some given that parts of this book are quite advanced.

Gripes

To keep things in perspective, this is the most impressive technical book I've read to date on any subject. But I can always find something to gripe about.

testing: IMO, you can't master regex without becoming effective at testing them. But this is not much addressed in the book. There's not a separate section on testing, nor even an index entry. One thing that Friedl does point out is that it's a good idea to watch out for unwanted matches.

The article I mentioned earlier by Stubblebine does begin to address testing regular expressions.

clerical errors: For whatever reason, there are an unusual number of typos in the book. However, none of the typos I encountered seem to me likely to mislead.

Perl 6: Perl 6 is mentioned in the book; but Perl 6 rules are not discussed, even though they rock. Perl 6 rules are evolutionary descendents (or perhaps I should say revolutionary) of what we call regular expressions in Perl 5, though I gather regular expressions in Perl and other computer languages long ago ceased to be regular expressions in mathematical terms.

Hm, Apocalypse 5 on "Pattern Matching" is dated June 4, 2002 and the second edition of "Mastering Regular Expressions" came out July 2002. Given the timing, any gripe here is unrealistic. Also, as of the writing of this review (in January/February 2004), Perl 6 has not yet been released.

Who is the Book for?

I recommend this book for anyone who

  • has used regexes for a while
  • is serious about them

You don't have to be a regex whiz to read this book. However, the book is demanding, if you aren't serious I wouldn't bother getting it.

Depending on your regex background, I have different things to say:

  • If you are a regex novice, I suggest focusing on chapters 1-3. IMO the book is worth the price for even the first two chapters.
  • If you are somewhere between a novice and a whiz, absorbing the material in this book should have a major impact on your proficiency.
  • If you are a regex whiz, I'm betting you'll pick up some things anyway, especially if you haven't kept up to date on the advances in various flavors of regex.

This is an impressive book. If you got all the way through this review, I suggest you have enough interest to get it.

Last Updated: 2004-02-05