Look Behind You

Apart from looking ahead, you can also look back (or "behind," as it's called in the world of GREP). The general format for positive lookbehind is (?<=).

Italicize Prefixed Words

For example, to italicize all words with the prefix over but not that prefix itself, do this:

Find what: (?<=over)\w+

Change to: <select italics at Change Format>

Italicize Letters Following a Year

For another example, we turn to bibliographies again. When an author has more than one publication in one year, thease publications are distinguished by placing a letter after the year (2007a, 2007b, 2007c, etc.) Many publishers want these letters italicized. To find these letters, all we need to do is formulate a lookbehind to match four digits:

Find what: (?<=\d\d\d\d)[a-z]

Change to: <select italics at Change Format>

We could have used \l (for "Any Lowercase Letter") instead of [a-z]—it doesn't make any difference.

Negative Lookbehind

Like negative lookahead, there's negative lookbehind as well. It, too, is straightforward: it tells InDesign to match things that are not preceded by a certain string. The general format is (?<!); to find digits not preceded by Chapter, use:

(?<!Chapter\s)\d

All rules applying to regular expressions apply to what you use in lookaheads and lookbehinds. Escaping special GREP characters is one of those rules. To find all dollar prices, for instance—that is, to find any string preceded by a dollar symbol and made up of digits, commas, and dots—use (?<=\$)[\d.,]+. As the dollar ...

Get GREP in InDesign CS3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.