GREP (regular expressions) can be a very powerful solution both for finding and formatting text that matches a specific pattern, within your InDesign documents. You could use GREP styles to apply a particular character style to all email addresses in a document for example, or you could use GREP to do something simple like remove extra spaces at the end of each paragraph.
The two main places where you can use GREP are in the GREP styles which can be found within the Paragraph Styles formatting options, and within the Find / Change options.
NB: If a GREP expression fails when pasted from a browser, it may include hidden or substituted characters. Just paste it into a plain text editor like Notepad first, then copy it from there into InDesign and it should work.
Here are some useful GREP examples that you might want to try, but feel free to add your own examples in the comments section.
Grep Style Examples
| Grep Style | Use |
| b(w+)s+1b | Find duplicate words that are right next to each other |
| £d{1,3}(,d{3})*(.d{2})? | Find a UK price formatted in various ways |
| b[[:alnum:]._%+-]+@[[:alnum:].-]+.[[:alpha:]]{2,6}b | Find standard email addresses |
| (?i)b(?:(?:https?://)|(?:www.))[[:alnum:]-]+(?:.[[:alnum:]-]+)+(?:/[^s<>()[]]*)? | Identify URL’s |
| (?<=[[:alpha:][:digit:]])s(?=[[:alpha:][:digit:]]+(?:[‘’-][[:alpha:][:digit:]]+)*[[:punct:]”’»]*r) | Prevent orphaned words at end of a paragraph, Create a character style with No break checked then use this pattern in your grep style to apply it. |
| (?<=d)(st|nd|rd|th)s | Use to add superscript to ordinal numbers such as 2nd, 15th etc.. Create a character style with superscript applied and then use this GREP style to apply it |
GREP to prevent weird line breaks
GREP can be useful to prevent certain content breaking onto a new line, for example when displaying a weight such as 100 KG you don’t want 100 on one line and KG at the start of the next line. Here are some grep patterns that can be used to apply a character style with the no break setting enabled in it.
| Grep Style | What it finds |
| b[aiAI]s | prevents lines ending with “a” or “I” |
| d+s?(kg|g|mm|cm|km|m|%|°C|°F)b | Prevents breaks like 100 KG splitting across lines. |
| (£|$|€)s?d+[.,]?d* | Keep numbers with currency |
| bd{1,2}s(?:January|February|March|April|May|June|July|August|September|October|November|December)b | Prevent breaks in dates |
| bd{1,2}:d{2}s?(AM|PM|am|pm) | Prevent breaks in time formats |
Find and Replace with Grep
GREP can also be used in InDesign’s Find / Change dialog box to look for content matching a certain pattern and replace it with something else. These styles can help with this
| Grep Style to Find | New Style | Use |
| b(w+)b 1 | $1 | Find and remove duplicate words |
| ^(.+r)1+ | $1 | Find and remove duplicates in a list |
| (“)(w+)(“) | $2 | Find text in quotes and remove quotes |
| (?(ddd))?{{-. }}?(ddd){{-. }}?(dddd) | ($1) $2-$3 | Format US phone numbers in standard format |
Disclaimer: The examples above have been collected over time from various forums, blogs, etc. and are not produced by ourselves. Unfortunately we didn’t keep a list of where we found them, and are not even sure who the original authors are, so can’t acknowledge them in this instance.