Favorite GREP Expressions You Can Use
You don't always have to figure out all the GREP codes yourself! Here's a bunch of examples to help you out.
This article appears in Issue 59 of InDesign Magazine.
GREP is an incredibly powerful technology for finding and replacing text. It shows up in several places in InDesign, notably the Find/Change dialog box and the GREP Styles feature. But you don’t always have to figure out all the grep codes yourself! Use the table below to find a grep expression that is close to what you’re looking for. You may need to tweak it a little, or combine some of these to achieve your task.
Then, if you have a grep code you want to share with others, feel free to add it to the comments below. If it’s really helpful, I may steal it and add it to the table in this blog post. Or, if I think of new ones, I’ll just update this table from time to time.
Note: Please don’t use the comments below to ask “what’s the grep code for…”? Instead, use our forums to ask those kinds of questions. Let’s focus the comments below on solutions, rather than questions.
And remember: you don’t need to memorize all these codes. Always look for that @ flyout menu!
Expressing Or
| (Milk|Dark) Chocolate | Milk Chocolate or Dark Chocolate |
| (m|M)ilk (c|C)hocolate | Milk Chocolate, milk chocolate, milk Chocolate, Milk chocolate |
| [mM]ilk chocolate | milk chocolate or Milk chocolate |
| [^m]ilk | ailk, bilk, etc., but not milk |
| (?i)milk | Case insensitive: Milk, milk, mILk, etc. |
| colou?r | color or colour (the ? means the previous character or expression may or may not be there) |
Wildcards
| [cC][\u]+e | Any string of letters that starts with c and ends with e, such as chocolate, Chocolate, case, coarse, etc. |
| \<c\w+e\> | Any whole word that begins with c and ends with e |
| c\w+e | Exactly the same as above, but using for ‘word boundary’ |
| +$ | All spaces at the end of a paragraph |
| +\Z | Remove spaces at end of story |
| [\u\d.]+@[\u\d.]+ | Simple e-mail address (e.g. da***@*************ts.com or **@**.net) |
| \d{4} | Find any four digits (the curly brace expression is not found in the InDesign @ menu) |
| [-\u\d._]+@[-\u\d_]+\.[\u]{2,4} | More ‘robust’ email (e.g. da*************@*3P.com) |
| \d+/\d+ | any fraction, such as 1/2, 22/7, or 355/113 |
| (?<!\d)(?<!/)\d+/(?!11)(?!0)(?!9)\d{1,3}(?!/)(?!\d) | Robust fraction grep: Will find fractions, but leave out dates such as 6/98, 9/11, or 6/17/2012. Unfortunately, it fails on 355/113 (which happens to be a reasonably good approximation of ?, proving that nothing is perfect). |
| [[=a=]] | Find any kind of a, no matter the accent, including a, á, à, ä, etc. |
| (?<=\d)(rd|th|st) | Find st, rd, or th ordinals (such as 1st, 2nd, 3rd) that follow a digit?in order to apply superscript to just the ordinal (not the digit). |
| (?<=\().+?(?=\)) | Find any text inside parentheses, but not including the parentheses. This uses ‘Positive Lookbehind’ and ‘Positive Lookahead’ |
Escape!
| \$\d\.\d\d | Find a dollar sign followed by one digit, a period, and two digits. Note that the dollar and dot have to be ‘escaped’ because they normally mean something different. |
| \(.+\) | Find anything inside parentheses (again, parens need to be escaped) |
| \Q(a+b*c?)\E | Escape everything; that is, find (a+b*c) exactly, without having to worry about escaping each special character. |
Fun Tricks
| Description | Find | Change to |
| Add an anchored object at the beginning of each paragraph (cut object to clipboard first) | ^(.) | ~C$1 |
| Reverse first and last name (but watch out for middle names or initials, too) | ^(\w+)([ .\w]+)? (\w+) | $3, $1$2 |
| After reversing the names (see above), fix any last names that started with Van or De. | ^(\w+, [ .\w]+) (Van|De) | $2 $1 |
| Find and remove duplicate words | (\w+) \1 | $1 |
| Find and remove duplicate paragraphs/lines in a list | ^(.+\r)\1+ | $1 |
| Find lists in which people have actually typed a number followed by a period followed by a space, and apply automatic numbered list style. | ^\d+\. ?(.) | $1 (and apply paragraph style that includes auto numbers) |
To learn more about GREP, visit: www.indesignsecrets.com/grep
This article was last modified on December 21, 2021
This article was first published on July 5, 2011

Comments are closed.