O'Reilly logo

GREP in InDesign CS3 by Peter Kahrel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required