Patterns again

It’s been a while since I mentioned Patterns, the $3 Mac application that lets you test and tune your regular expressions.

Patterns example

The best thing about Patterns is that it puts your regex testing outside the rest of your program, so you know that any errors are coming from the regex itself, not any other misbehaving part of your code. You can, in theory, do the same thing by writing a little skeleton program that tests your regex, but if you’re anything like me, you don’t have the patience for that. Three bucks is a small price to pay for prebuilt testing environment.

One thing you should know before using Patterns: No matter which language is chosen in the dropdown menu up near the top right corner, Patterns always uses Perl-style references for captured groups in the replacement section, e.g., $1 for the first parenthesized group, $2 for the second, etc. So even if you’re writing in Python, as I usually am, you can’t use Python’s syntax, which uses backslashes instead dollar signs. This is less annoying than you might think—so many languages and utilities use dollar signs, I’m used to switching back and forth between the two styles. For example, the regex above is from my Keyboard Maestro macro for quick image links; Keyboard Maestro uses Perl-style group references.

If you’re not familiar with regular expressions, you should be, especially if you’re interested in automating your work with a computer. They are a sort of mini-language for describing patterns of text, and knowing them will greatly expand your ability to search and replace. I first learned regex syntax back in the mid-90s from the “Searching with Grep” chapter in the BBEdit manual (a chapter written by John Gruber, if I remember correctly). If you don’t own BBEdit, there are millions of regex tutorials on the web. I suggest David Mertz’s short introduction. I have his Text Processing in Python book, and he’s a clear and succinct writer. If you really get into regexes, you can move on to Jeffrey Freidl’s book.

The conveniences that Patterns brings to debugging regexes for us old hands should be even more helpful to those just learning. I wish I’d had it around 20 years ago.

Update 8/1/15 9:20 AM
I am informed by a reliable source that Patrick Woolsey was the original author of the Grep chapter of the BBEdit manual and that John Gruber expanded it when it was revised for Version 6.5. Since BBEdit 6.5 was released in 2001, during my years in the Linux wilderness, it’s Patrick that deserves the credit for my initial regex instruction. Patrick still works at Bare Bones and is likely to be your first line of support when you email for help. Now I understand why his responses are so well written.