What is GREP?
GREP (Global Regular Expression Print) lets you find and replace kinds of text, not just exact words.
- Normal Find/Change: find “cat”, replace with “dog”
- GREP Find/Change: find “two or more spaces”, replace with “one space”, wherever they are
Where to find GREP in InDesign
- Press
Ctrl+F - Click the GREP tab
- Type the pattern in Find what and the replacement in Change to
- Click Find Next a few times, then Change All
Your first pattern EASY
Start with the most common problem in manuscripts: double spaces. Typing two spaces in Find works, but only for exactly two. GREP handles any number:
{2,} means “two or more”. It’s called a quantifier, and you’ll use it all the time.Wildcards and special characters EASY
Wildcards stand for a type of character:
| Pattern | Means | Matches |
|---|---|---|
. | Any single character | a, B, 3, @, space |
\d | Any digit | 0 to 9 |
\w | Any word character | letters, digits, underscore |
\s | Any white space | space, tab, return |
\t | Tab | the tab character |
\r | Paragraph return | end of a paragraph |
\n | Forced line break | Shift+Enter |
All of InDesign's special characters, with the token for each and how InDesign shows it: InDesign GREP special characters.
. * + ? ^ $ [ ] ( ) { } | \. To find them as plain text, put a backslash in front: \. \* \?Quantifiers: how many? EASY
| Quantifier | Means | Example |
|---|---|---|
* | Zero or more | ab*c finds ac, abc, abbc |
+ | One or more | ab+c finds abc, abbc, not ac |
? | Zero or one | colou?r finds color and colour |
{3} | Exactly 3 | \d{3} finds 123 |
{2,4} | Between 2 and 4 | \d{2,4} finds 12, 123, 1234 |
{2,} | 2 or more | \s{2,} finds runs of white space |
Anchors: where? MEDIUM
Anchors match a position, not a character:
| Anchor | Means | Example |
|---|---|---|
^ | Start of a paragraph | ^The finds “The” only at the start |
$ | End of a paragraph | \.$ finds a period at the end |
\b | Word boundary | \bcat\b finds “cat”, not “category” |
Groups and capturing MEDIUM
Parentheses ( ) remember what they found, so you can reuse it in Change to as $1, $2, and so on. $0 is the whole match.
Practical examples
Patterns you can use today:
Typography
Cleanup
Find many more in the GREP Library, each with a before and after example.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Forgetting to escape | . matches any character, not only a period | Use \. |
| Greedy matching | ".*" runs from the first quote to the last one in the paragraph | Use ".*?" for the shortest match |
| Case | the doesn’t find “The” | Use [Tt]he or (?i)the |
| A space that finds nothing | A space typed in Find is a regular space. Em, en, thin and nonbreaking spaces are other characters, each with its own token | Turn on Type, Show Hidden Characters to see which space it is, then use its token: ~m for an em space, ~S for a nonbreaking space, or \s for any white space. All space tokens |
| Change All blindly | A wrong pattern changes hundreds of places you didn’t mean | Click Find Next a few times first, or use Preview in GREP Batch Processor |
Resources
| Resource | What it is |
|---|---|
| Treasures of GREP | Facebook group where InDesign users share and improve GREP |
| GREP in InDesign | The classic book by Peter Kahrel |
| CreativePro | Articles and tutorials, including the former InDesignSecrets |
| Regex101 | Online regex tester. Close to InDesign GREP, but InDesign codes like ~_ or ~S are not supported there |