Repeat: Sequences of Characters

Without GREP, 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. GREP makes this much simpler; 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). We’ll deal with them in turn.

? Zero or One Time

To find, say, both British and American spellings harbour of harbor, search for harbou?r. 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 (matched by \d\d), 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.

The ? operator is useful for matching numbers as well—for example, in dates. The expression \d\d-\d\d-\d\d\d\d finds dates formatted like 08-04-2007. But dates come in different shapes, so to allow ...

Get GREP in InDesign 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.