Look Around

Lookaheads and lookbehinds can be combined. This is called "lookaround," though not in InDesign's interface. No matter. A few examples will illustrate this.

Replace Certain Hyphens with En Dashes

To match only those hyphens that are preceded and followed by a digit and replace them with en dashes, use these expressions:

Find what: (?<=\d)-(?=\d)

Change to: ∼=

Match Words Wrapped in Certain Characters

And to italicize all words in a document that occur in double quotation marks but not the quotation marks themselves, use these expressions:

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

Change to: <select italics at Change Format>

Keep Certain Words Together

In Polish typography, the single-letter prepositions w and z (both are general locative prepositions meaning "in", "at", and "by," and "from", "off", respectively) cannot be separated from the following noun or adjective. The neatest way to format them is to replace the space between the two words with a variable-width nonbreaking space, which can be done as follows:

Find what: (?xi) (?<=\b[wz]) \s (?=\w)

Change to: ∼S

First note that we use the free-spacing modifier again for clarity. We added i to make the search case-insensitive so that W, w, Z, and z are found. The notation here is shorthand for (?x)(?i), which works equally well, but the contraction makes the expression a bit easier to read. The find expression matches spaces that are preceded by w or z and followed by any word character. Naturally, in the lookbehind, we need to specify the word ...

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.