Favorite GREP Expressions You Can Use

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][\l\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
\bc\w+e\b Exactly the same as above, but using \b for “word boundary”
\s+$ All spaces at the end of a paragraph
\s+\Z Remove spaces at end of story
[\l\u\d.]+@[\l\u\d.]+ Simple e-mail address (e.g. david@indesignsecrets.com or d2@AB.net)
\d{4} Find any four digits (the curly brace expression is not found in the InDesign @ menu)
[-\u\l\d._]+@[-\u\l\d_]+\.[\u\l]{2,4} More “robust” email (e.g. david-blatner_2@63P.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 \b(\w+)\b \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

Tags
Related Articles
Comments

67 Comments on “Favorite GREP Expressions You Can Use

  1. Andrea,

    do this:

    create a character style, call it No Break or something similar. Assign it as No Break in the Basic Character Format options.

    Search: – (?=[\u\l])|(?<=[\u\l]) -

    and replace with this character style or add this GREP style into the paragraph style options. This way whenever you have this combination it will be assigned no break automatically.

    Michael

  2. @Michael: Add (.) at the end of your search string, and put $1 in the Replace field.
    If the Replace field is empty, ID only applies formatting. This way, you force a replacement, if only for the single character you are picking up from behind your digit.

  3. I have a GREP challenge I can’t solve – any ideas?

    I need to apply a different style to the last bullet in a list (to allow more space after than the regular bullet style).

    I thought I could search for bulleted style followed by a regular paragraph style, but I can only search for one para style at a time.

    Does anyone know how to do this?

  4. I wanted to apply small caps to all capital letters in a paragraph, but not to those makred in bold (or another character style).

    Is there a solution via GREP styles?

  5. ok, I’m new to GREP.. I need a GREP that will change the style of a URL within the body copy of a document. I’ve created a style. But I can only get the first three characters to change (www). Below is what I have, but I don’t know how to get the remaining information to change.

    [www/./]

  6. @Stacy: The “how to target a URL” is one of the longest-standing discussions in GREP history, I think. :) Here’s the one that John Gruber came up with at Daring Fireball (click link to see his explanation):

    (?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`!()\[\]{};:'".,<>?«»“”‘’]))

  7. David, thanks. That’s exactly what I needed. I made one modification:

    (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|(http|ftp|www)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()]+|\(([^\s()]+|(\([^\s()]+\)))*\))+(?:\(([^\s()]+|(\([^\s()]+\)))*\)|[^\s`!()\[\]{};:’”.,?«»“”‘’]))

    (http|ftp|www) – added http and ftp into the string which allows me to search and change all three.

  8. Hi I am trying to come up with a search for add Index entry for Each Text box in my documents (i have aproximaly 1000 text boxes…)
    Grep find/change don’t add index entry…

    Help Please

  9. Hello, I need help in this. I want to delete every letter variation after word BEX. For instance Title-BEX-14 char-style-override-8 should become Title-BEX . How can I do this?

  10. @Rain: You can probably look for \$\d+\.\d\d (dollar sign, one or more digits, dot, two digits). No?

    @Norman: You are correct that InDesign doesn’t let you do this. Look at the IndexMatic script at indiscripts.com

    @burak: Perhaps something like BEX.+ (which means BEX followed by every character until the end of the paragraph)

  11. Hello – – this great script finds all the text after a tab and before a return. Works like a dream in my paragraph style, thank you…

    (?<=\t).*?(?=\r)
    What I woudl love to find out is how to tune the script to find all the text between TWO consecutive tabs and a return…

    Would be incredibly useful; trying to assign a character style to numbers in an index…

    thanks for your time and assistance!

    Original script found here:

    http://indesignsecrets.com/findbetween-a-useful-grep-string.php/commen t-page-2#comment-550882

  12. If I want to find something after the beginning of a pragraph, I use symbol “^”, but…

    What if I want to find a character that IS NOT in the beginning of a paragrah?

    Thanks

  13. I’m trying to find the beginning of a paragraph to end of paragraph with a specific style. Then I want to insert a tab in front of the found text. I need an actual tab character (i can’t just change it in the style). Any assistance would be greatly appreciated.

    Thanks.

  14. After tons of research I answered my own question by reading up on some of the posts – I should have just done that in the first place.

    Find: \<.+
    Find Format: "my para style"
    Change: \t$0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>