Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

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

157 Comments on “Favorite GREP Expressions You Can Use

  1. I have a question about combining multiple GREPs.
    In my special case i would like to replace every “Normal Space” between a value and it’s unit ( g, ml, kg, L, a.so.) with a “Thin Space”.
    What i have so far is a replacement for every ” g”:

    Search:
    \b˜sg\b|\b g\b

    Replace
    ˜<g

    But actually i have to adapt this expression to every possible type of a unit (g, kg, l …) and run the replacement again. A bit unhandy. How can i combine this into one single replacement-expression?

    Best regards
    Volker

  2. I would suggest that you use GREP in styles for this, rather than a find and replace.

    Your expression in your Paragraph Style would look for a number, space, measurement (Ex. 9 ml) and by use of look behind and look ahead, you would narrow it down to apply a Character Style to just the space between the number and measurement. Set your Character Style to something like 50% horizontal scale, thus automatically thinning all spaces in your document that fall between a number and its measurement.

  3. I need an expression to select the strings between parenthesis (included) at the end of a paragraph.

    Example:
    Paragraph starts here (I dont want to select this one) because is has some text after it (but I want to select this) (and this) (and as many I have at the end of a paragraph).

    I’m a begginer ih this theme, Could you help me, pleeeeease? :D

  4. Luiza,

    You can find the first instance of something but not the last. So, you can’t do it with one grep expression. However two will do the trick.

    Grep Style 1
    (?<=\().+?(?=\))
    This one looks for one or many characters between and open and close parentheses. It will find and style all within your paragraph.

    Grep Style 2
    (?<=\()[\s\l\u]+(?=\))
    The second will go back over and style only the first instance.

    So basically the character style for Grep Style 2 should match the rest of the paragraph body.

    • Thank you very much! I’ll try it!
      I’m sorry to annoy you, but would this Style2 also work if I had something like:

      blabla (dont select) blabla (dont select) blabla (select)(select)(select)

      Or would it select only the first (dont select)?

      • Luiza,

        Sorry wasn’t thinking. It should be:

        Grep Style 1
        (?<=\().+?(?=\))
        This one looks for one or many characters between and open and close parentheses. It will find and style all within your paragraph.

        Grep Style 2
        ^.+?\)
        The second will go back over and style only the first instance.
        So basically the character style for Grep Style 2 should match the rest of the paragraph body.

        In regard to your comment, style 1 will style all instances and style 2 will style only the 1st instance.

        If you are looking for something more specific, we would have to know more about the content that resides within these.

    • Hi Justin,

      “You can find the first instance of something but not the last.”

      Do you know how I can find the first instance of a registered trademark symbol?

      Thanks,

      Holly

  5. Hi Justin, thank you again. I used this last solution, but it still doesn’t fit all my needs.

    Before asking you, I was using this: \(.+\)$
    But it was selecting everything between the first “(” and the last “)”

    A real paragraph would be this (sorry it’s in portuguese), where I wanted to style only “(Redação..)” and “(Revogado)” but not those two “(dois)”

    “Considera-se caracterizada a atividade preponderante referida neste artigo quando mais de 50% (cinqüenta por cento) da receita operacional da pessoa jurídica adquirente, nos 2 (dois) anos anteriores e nos 2 (dois) anos subseqüentes à aquisição, decorrer de transações mencionadas neste artigo. (Redação dada pelo Ato Complementar nº 35, de 1967) (Revogado).”

    • Luiza,

      At this point start to put bandaids on the issue.

      You could add a third style, similar to style 2, which looks for “dois” or other numbers.

      \(dois|três\)
      additional numbers separated by a pipe.

      Someone else may have a better idea; however, Grep is not a comprehensive solution. Consider a character style with a keyboard shortcut as an alternate.

      • Justin/Luiza,

        Could this do the trick?

        1st grep style:
        (?<=\().+?(?=\))

        2nd grep style:
        (?<=\().+?(?=\) \w)

        The first one changes all text between parens to what Luiza wants the last parens text to be.
        The second one fixes the earlier parens text back to normal because it only changes text between parens where the end parens is followed by a word character. Therefore, any that are followed by another parens or punctuation will not be effected.

        This worked in my testing based on the sample text Luiza provided. Considering it's in a different language, I don't know if there is anything I can't foresee that would prevent this from working.

  6. How do I swap two numbers using GREP styles?

    Year at the beginning of the paragraph and a rating at the end. I have been using Grep find and replace – (\d{4})(.*)(\d\d\r\Z) to $3$2$1 but I need to use styles and then use NEXT styles to style all the text.

    cheers
    Adep

  7. Hello,
    my file is something like:
    shiva
    cool
    april
    inverter
    cool
    hot
    cool
    nmo
    cool
    Now i want to grep data between april and first coming cool after april including these words. i want a general solution so that it can apply to all files.
    Kindly help.
    Thanks in advance

  8. Need to Italic the words between the codes:

    @8:Aggression, the Crime of Crimes

    Words change coding does not. It’s for an index. How do I set up my find/change?

    • Deleting code when I post. Basically Ihave code on both side of the copy. Telling me that it should be italic. I want to do a find/change

      • ydesign5: I’m not sure exactly what you’re trying to do, but this image should show you how you can make some text in-between two html “em” tags italic:

  9. I am working on an Islamic calendar. We put 6 praying hours for 20 cities every day as a list. Now i must change the order of the cities and i can use only 9 dollar signs for replacing. Is there a way using more then 9 dollar signs?

    • Ali: I don’t know how you can do more than 9, sorry. But I wonder if there is a better way, using excel or something. That is going to be tricky.

  10. Hello

    How would I get GREP to find first capitalised word (and those following) up to a beginning parenthesis e.g.

    Mathematical methods, models and modelling (MST210)

    So it would select “Mathematical methods, models and modelling” but only that?

    Many thanks

  11. Here’s an example:

    You’ll begin with one of two modules: Object-oriented Java programming (V000) or Designing applications with Visual Basic (V000). Next you’ll learn how to analyse requirements, plan, design, implement and test a range of web applications in Web technologies (V000).

    • Cross-references or hyperlinks might be one solution. Doesn’t automate the process but you would be adding interactive features and the ability to adjust the style of these references at a later date.

    • Andy: I would put this in the “almost impossible” category because there just isn’t any difference between a normal sentence and the class name.

      But okay, here’s an attempt. If this doesn’t work, start up a query in the forums instead (those are better for back and forth, rather than comments at the end of a blog article).
      \u[^.,:]+?\(\u+\d+\)

      That will find an uppercase letter followed by any number of characters that are not a period, comma, or colon, then followed by parentheses containing only uppercase letters followed by numbers.

      But this won’t work for your first example because the course name has a comma in it! You could remove the comma from the “not” statement, but then won’t find other ones properly.