Short Review and RecommendationTitle: Mastering Regular Expressions
Notes:
Smiley Rating: :) :) :) :) :) of 5. If regular expressions are important to you, get this book. "Mastering Regular Expressions" is exceptional in the scope of what it covers regarding regular expressions, in it's thoroughness, and in it's perspective. To date this is the most impressive technical book I've read on any subject. It's a virtuoso performance. The above was originally written about the 2nd Edition, but it's equally true of the 3rd Edition. One caveat: parts of the book are quite demanding. See my longer review for more detail. Again, if regular expressions are important to you, get this book. George Woolley of Oakland.pm [top] MiscellaneousChapter Titles
Notes:
[top] Some Perl FunThis book is not a Perl book, but Perl is prominent in both regular expressions and in this book. I'm expecting that many of the readers of this review will be into Perl. Below are some links to Perl humor:
[top] Two Related BooksThe Second Edition of This BookNotes:
A Related ReferenceNotes:
[top] A Related ArticleFive Habits for Successful Regular Expressions Notes:
[top] |
Longer ReviewContents
Note:
The TitleThe 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:
but also
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? What I can say is that the author sure motivated me. Parts of the book were quite difficult for me, but I've now been through all of it. Some of the things contributing to my motivation have been:
[top] About the ReviewerBiases: Some of my biases that could effect this review are
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. One thing though, I now use PHP a lot. Oh, I owned 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. I have an even higher regard for the 2nd and 3rd editions. Expectations: When I read the 2nd edition, what I was hoping for was
When I read this 3rd Edition, what I was hoping for was mainly to improve my use of PHP regular expressions. [top] Relation to Second EditionThe second edition came out in 2002, the third in 2006. That's a period of four years. Many things have happened in that time. Two that had a significant effect on the 3rd Edition are:
The following table gives a crude picture of the relationship between the chapters of the second and third editions. I've included the Preface and Index in the table as I think they are important.
Notes:
[top] What I Learned about Writing RegexesNote:
Well, I learned a great deal. Even before my review of the 2nd Edition was complete, I began using the following in some of my Perl code:
Before reading the 2nd edition of 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, .NET, PHP 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. [top] What I Learned about Teaching RegexesNote:
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. I give a specific example below. But more important I now have a book to recommend to novices who are serious about regexes. (Of course, I also recommend this book for people who are experienced with regexes.) 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. A Book for Novices: I have become convinced that this book could be useful for a Perl novice who:
For such a person, I'd suggest the following use of the book:
My Point: This book can be useful to you in helping novices. The book certainly has been useful in teaching the regex novices I've encountered 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. The author is super clear when dealing with basics. [top] What I Got from the PHP ChapterNote:
PHP actually has 3 regular expression engines. This book focuses on the regular expressions supported by the preg regex engine because the author thought it the best of the three with respect to features and with respect to speed. When I started using PHP, I quickly determined I preferred the preg engine. preg stands for "Perl Regular Expressions". preg uses the PCRE ("Perl Compatible Regular Expressions. The regular expressions supported by the preg engine are quite similar to those in Perl. However, at least from my point of view, the way preg regular expressions are integrated into PHP is quite different from the way regular expressions are integrated into Perl. I got a number of things from the PHP chapter. Below I'll discuss two of them as examples. preg Function Descriptions: One of the best things about the PHP chapter is the descriptions of the preg functions. These descriptions are in far more detail than the corresponding descriptions in my favorite PHP books. The descriptions take up 22 pages (to describe 7 functions). I've already found these descriptions useful. The function of most interest to me is preg_replace. One thing I learned is that I can pass this function an array of patterns and a corresponding array of replacements and it will apply all the patterns and when the patterns match perform the corresponding replacement. I wasn't aware you could do that. The PHP Chapter is the best treatment of preg functions I've seen anywhere. Similarly, the Perl Chapter is the best treatment of Perl regular expressions I've seen anywhere. I expect to use these chapters for reference as time goes on. The author makes it clear that he didn't intend the book as a reference. However, it is much better than anything else I'm aware of for this, so I plan to use it that way. I'll refrain from commenting on the Java and .Net chapters since I've only used Java a little and I have never used .Net. Recursive Regular Expressions: preg has a special notation for recursive expressions. For example, the author suggests the following regex for checking that parentheses are balanced: ^((?:[^()]++|\((?1)\))*)$ The (?1) says the whole first grouping may be applied where it occurs in the pattern. You don't need to understand this notation now (though if you do, cool!). Just understand there is a simple notation for recursion in preg regular expressions and it is explained in the PHP chapter. My Point: If you use PHP preg functions much, I bet you'll find the preg function descriptions in this chapter useful. You may not be interested in using recursion in preg, but this is just one of many things explained in this chapter. [top] GripesTo 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 regexes 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 for testing. 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. Not Optimum as a Reference: This book is not intended to be a reference. However, it's such an excellent book and covers so much, that (as things stand) it makes sense to use it that way once you've worked through it. I can think of two ways the publisher could address this:
Perl 6 not Included: I think 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. Here we are in 2006 and Perl 6 has still not yet been released. OK, I'll reframe this as a wish: If Perl 6 or some other languages makes a release including anything like Perl 6 rules, my wish is that the author update his thinking in this area with a new edition of the book, an article or some such. [top] Who is the Book for?I recommend this book for anyone who
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:
[top] Final ThoughtThis is an impressive book; it's easily the best book I've encountered on regular expressions. If you got all the way through this review, I suggest you have enough interest to get it. Smiley Rating: :) :) :) :) :) of 5. [top] |
Completed: 2006-08-31