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:
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.
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.
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.
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.
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.