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.
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.
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.
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:
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.
Yeah, they are definitely awesome! They just make solving so many problems much easier.
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.
Thanks, Ben (and Craig)! I've got to give props to coauthor Jan Goyvaerts though, who wrote a lot of the sections that you referenced.
Well, a great book, to be sure.
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.
I have not read it personally.
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.
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!
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.
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.)