You know the maxim, “Give a man to fish and he eats for a day; teach a man to fish and he eats for a lifetime”? A similar adage applies to anyone who spends a lot of time working with text on their Mac: Learn how to use regular expressions—sequences of characters and variables that you can use to perform powerful searches—and you’ll benefit greatly, for a very long time.
I’ve saved hours or days—maybe even weeks—of time over the past decade or so by using pattern-matching search-and-replace actions on text. Regular expressions (also called regex or grep) form the common language that most programs use for these actions. Specifically, I’ve been using grep in BBEdit, crafting complex patterns and then pressing the Find button and hoping that my pattern is right. But it isn’t always—learning regex takes practice and involves lots of trial and error.
Nikolai Krill’s $3 Patterns (Mac App Store link) simplifies the act of creating regex search patterns, and it provides a live view of the results of those patterns.
To use Patterns, you first paste (into the Search Text field) the text you’re planning to search. Then you enter (in the Regular Expression field) a regular expression, which is displayed in large, easily readable type. As you type your expression, Patterns dynamically highlights matching text in the Search Text pane.
If you’re planning to replace the found text, you can test that out, too. Click the Replace tab and a Replacement field appears, with the lower half of the Patterns window morphing into a side-by-side, before-and-after view. The Search Text pane on the left still highlights all matched text; the new Replaced Text pane on the right shows the results of your replacement pattern.
What if you can’t remember the particular pattern you need? Even an experienced user of regular expressions can forget exactly which symbols to use in particular circumstances. Patterns offers a built-in regular-expression reference, in the form of a floating palette. It’s not going to teach you everything about regular expressions—for that I strongly recommend Mastering Regular Expressions by Jeffrey Friedl, which is how I learned—but it’s handy to have around.
I do wish Patterns would let you open a text file directly rather than requiring you to copy and paste your text. I’d also like a way to save regex patterns for later reference or use. And perhaps my biggest complaint is that Patterns doesn’t offer you a way to view the sub-matches of your pattern. One of the most powerful aspects of regular expressions is that you can capture text by surrounding the matching patterns in parentheses, but there’s no way in Patterns to fine-tune the text that’s being captured. Instead, you have to just build a replacement pattern and look in the Replace Text pane to see if that pattern has been rendered properly.
Still, for $3, Patterns is a helpful addition to the toolkit of anyone who builds regular expressions with regularity. If you work with text, you owe it to yourself to learn regular expressions—and using Patterns can help you figure out exactly what your patterns are capturing.
(Of course, you don’t need a dedicated Mac app to test out regular expressions. The website regexpal does it in a web browser, and the excellent—and free—text editor TextWrangler offers a complete regular-expression-based search-and-replace system. But for $3, Patterns’s native Mac interface and search-and-replace preview are worth the paltry premium.)