Discover legacy content from FontShop.com, preserved for your reference.

FontShop
Please update your browser. Why?

Find and Replace, GREP

July 25, 2013 by David Sudweeks

Alright, this isn’t necessarily about using type—more like using text—but hang with me and you’ll find the information is still applicable if not ultimately very handy. Much of the initial work of typesetting is rearranging and stripping the junk out of your text, including removing that pesky second space after each period.

Find and Replace

The way most go about it is to replace two spaces with a single space, and repeat the action until the search reveals no matches. You’ve likely grown comfortable with Find and Replace by now, but if your text editor allows for finding and replacing with regular expressions, (InDesign does) you may be surprised by the added potential this offers you. Combined with InDesign’s additional ability to apply paragraph and character styles, you’ve got a powerful tool at your disposal.

Slightly more advanced

Grep, a popular term for searching, finding and replacing text that fits a given pattern, allows you to do the above example in a single step, among other, much more complicated and ostensibly more useful things. In InDesign, GREP sits beside the Text Find/Change dialog, looking inconspicuously similar. The below example shows how to find a period followed by one or more spaces, and replace that pattern (or expression) with a period and a single space.

Above To find the period, precede it with a backslash (the period has its own special meaning in regular expressions; the backslash says, “nope, just looking for a plain old period,”). This is followed by one space, a second space and an asterisk, (meaning one space, followed by zero or more spaces). When this expression is found, we change it to a period followed by a single space. Similarly this method can be used to replace multiple paragraph breaks:

\p\p*

with a single paragraph break:

\p

or any similar simple substitution.

Replace it with the thing I found

Sometimes when I’m finding and replacing, the thing I want to put in the Change to field is based on what I just found. Say I’ve got a list of names, Last comma First, e.g. Tanner, Jerry; that I want to change to First Last, Jerry Tanner. With Grep, just specify that everything preceding the comma is the (first pattern), everything after is the (second pattern), and then change it to (second thing) (first thing).

Above I use a simple expression: One pattern of zero or more characters, followed by a comma, space, and a second pattern of zero or more characters. The period means any character, with certain exceptions. The two patterns are grouped in parentheses, making it possible to recall them later in the Change to field. Dollar sign 1 is the first pattern in the expression, which we put last. The comma space we ignore. And the last pattern we put first with dollar sign 2. Since the period character doesn’t include paragraph breaks, all we have left to do is hit Change All. Before and after below.

With this level of knowledge, you’re ready to hack into all kinds of text files, turn hyperlinks into labeled URLs, you name it. Next week I’ll go into a bit more styling, including how to apply styles with this and the paragraph style dialog.

In the meantime, how does one learn more about Grep? Adobe has a good resource page on Grep specific to InDesign. There’s also a great general beginners guide  written by Jan Goyvaerts I recommend. Some potentially unfamiliar terminology such as escape and literal will pop up here and there, so keep a programming reference handy, or Wikipedia. Study up on what Grep can do, and you’ll find there’s almost no bottom to the complexity, but—and I’m warning you—don’t get caught up in it. A working understanding of the basics will immensely improve your ability to find and change text. From there, it’s a long descent into the weirdness of cold hard logic and diminishing marginal returns.

Thanks for reading. Using Type continues here Thursday. Thanks to Nicole Dotin’s Elena and Thomas Gabriel’s Premiéra for the illustrations.