Repeat: Sequences of Characters

In InDesign CS2, to replace sequences of space characters with just a single space, you had to repeatedly replace two spaces with one until there were no more double spaces left. In CS3, there is a much better method: search any sequence of more than one space and replace with a single one: find \x{20}\x{20}+ and replace with \x{20}. As we've seen earlier, the plus is a repeat character and means "at least once," so that the expression paraphrases as "find a space followed by a sequence of at least one space." There are several repeat characters; they're all in the Repeat flyout (see Figure 7).

Repeat options

Figure 7. Repeat options

? Zero or One Time

To find British and American spellings of, say, harbour, search for harbou?r, which finds harbour and harbor. The scope of ? is just one character, so in this example it applies only to the u. The expression paraphrases as "find harbo, perhaps followed by u (or, followed by zero or one occurrence of u), followed by r. To extend the scope of ? to more characters, group them with parentheses: (19)?\d\d finds all two-digit numbers, as well as four-digit numbers whose first two digits are 19. You can combine this with alternation: (18|19|20)?\d\d matches two-digit numbers, as well as four-digit numbers that begin with 18, 19, or 20.

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.