Using GREP within InDesign

0800 156 0777

Using GREP within InDesign

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.

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+)\b \1 Find and remove duplicate words that are right next to each other
\£[\d.,]+(XV)? Find a UK price formatted in various ways
[\l\u\d.]+@[\l\u\d.]+ Find standard email addresses
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»????])) Find a URL
(?<=\w)\s(?=\w+[:punct:]*\r) Prevent orphaned words at end of a paragraph. Apply a character style with No break checked to the last whitespace character in a paragraph.
(?<=\d)(st|nd|rd|th)\s Use to add superscript to ordinal numbers such as 2nd, 15th etc..

  Find and Replace Grep Examples

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
\(?(\d\d\d)\)?[-. ]?(\d\d\d)[-. ]?(\d\d\d\d) ($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.