GREP by Example
It may be easiest to see how GREP works with a few examples. But first a quick comparison with InDesign’s Text search, which is useful because the comparison reveals GREP’s strength.
InDesign’s text search is used mainly for searching literal text: when you search for cats, you find just that (disregarding settings such as case-sensitivity and whole-word only). But in the Text tab you can use some wildcards: ^9 finds any digit, ^$ stands for any letter, ^? matches any character, and ^w is used to find any whitespace. Thus with Figure^w^9 you search for the literal text Figure followed by any space, followed by a digit. When you use any of these wildcards, you’re no longer looking for literal text, but for a pattern. The four wildcards in the Text tab are useful, though rather limited—for instance, you can’t use them for replacements, only for searching.
In contrast, with GREP you mainly look for patterns. For example, you can look for series of digits rather than for a single digit. Figure\d+ matches the literal text Figure followed by any number (2, 34, 121, etc.): \d stands for digit, the plus sign means “at least one.” The GREP expression \u\l+ finds an uppercase letter \u followed by one or more lowercase letters \l+. GREP also deals with simple alternation. For example, to find both centre and center, search for cent(re|er); alternatives are separated by pipe symbols (|). Optionality adds more flexibility: to find both the singular and plural forms of these ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access