Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

Patterns review: Utility makes quick work of regular expressions

Jason Snell | March 20, 2014
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.

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.

If you're building a search-and-replace pattern in order to use it in code, Patterns has you covered there, too. A pop-up menu lets you choose from several different languages, including C#, Perl, PHP, Python, Ruby, and JavaScript. Then just click the Copy Code button and your regex pattern will be placed on the clipboard, formatted properly for the chosen language.

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.

 

1  2  Next Page 

Sign up for CIO Asia eNewsletters.