Matt Gemmell

My new book CHANGER is out now!

An action-thriller novel — book 1 in the KESTREL series.

★★★★★ — Amazon

Find Patterns in text on Lion

Guide, Interface, Software, Tech & User experience 2 min read

If you often need to search for certain patterns of text rather than exact, unchanging pieces of text, there’s a new feature in Mac OS X 10.7 “Lion” (read my review of it at The Guardian) which will be useful to you. This brief article describes how it works.

If you have Lion and want to follow along, you should launch the TextEdit application and type some text into a new document. Here’s the sample document I’ll be using:

Our sample document, containing the text: My email address is matt at mattgemmell dot com and you can also use matt at instinctivecode dot com

Trigger the Find function by pressing Command-F, or by choosing “Find…” from the Find submenu of the Edit menu. A find bar will become visible at the top of your document. Click the magnifying glass in the find bar, and a menu will appear.

The 'Insert Pattern' option, available in the popup menu attached to the magnifying glass in the Find field, or by pressing Command Option Control P

Choose the “Insert Pattern” option, and you’ll see a further popup menu listing several common patterns of text. Choose whichever patterns you like. In my example, I want to just get the raw email address from the document, so I’ve used the “Any” and “Email Address” patterns in that order, to grab both the text before each address, and the address itself. Any patterns you insert will automatically be colour-coded.

The patterns menu, showing all available patterns

Next, make sure the Replace checkbox is checked, and you’ll see an extra field appear below the find bar, letting you enter text to replace the found patterns with. The “Insert Pattern” command works here too, and produces a slightly different set of options.

The replacement patterns menu, showing any matched patterns we used in the Find expression

Notice how the colour of the replacement patterns in the menu match the colours of the pattern tokens in the find field. This is because, if you use one of those patterns in the replacement field, the actual text that matched that pattern will be inserted into the document. This is what makes pattern-based find and replace so useful and powerful.

In my example, I chose to replace the entire found text with just the email address that it contained, plus a line-break character - so that the end result would be a document containing just the email addresses, with one on each line.

The final document, containing two email addresses separated by a line break

You can experiment with the other patterns to see what’s possible. If you discover that your needs are more advanced than the patterns will allow, it’s worth knowing that these patterns are actually examples of a technology called regular expressions, and that many text editors on the Mac support them.

To learn more, you might want to read a tutorial on regular expressions, and then to find a text editor app that will let you use regular expressions natively (such as BBEdit). You could also search for info on grep, which lets you use regular expressions on the Unix command line.

Lastly, if you’re a Mac developer and you want to make use of this functionality in your own apps, here’s the relevant API documentation.

If you’re interested in more tips about OS X Lion, I’ve been tweeting extensively about it recently; you may want to follow me (@mattgemmell) on Twitter.