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

Speed Up Your GREP Styles

by Peter Kahrel

Fact: Searches and replacements done from InDesign’s GREP tab in the Find/Replace window are blindingly quick.

Another fact: GREP styles are a great time-saver—some well-designed GREP styles can save you a lot of formatting. However, a third unfortunate fact is that long documents can become sluggish if they contain many GREP styles. If you suspect that GREP styles may have something to do with a document’s sluggishness then it’s time to organize the GREP styles better and to optimize as much as possible the GREP expressions used in the styles. Here are some tips to speed up GREP styles.

1. Avoid parentheses as much as possible.

You can achieve the biggest speed gain by using parentheses only if you absolutely have to.

Parentheses are used in GREP expressions (a) to create references and (b) to group items. (“References” are the parts of a GREP expression which you refer to using $1, $2, etc. in the replacement expression.) If you have to create a reference, then obviously you have to use parentheses. But grouping is often used unnecessarily, especially in lists with alternatives. For example, to find the words Figure, Map, and Graph followed by one or more digits, you would use this expression:

Figure|Map|Graph \d+

There is no reason at all to group the alternatives, as in

(Figure|Map|Graph) \d+

This expression creates a reference to every match, and that takes time. And though InDesign’s GREP processing is very quick, the accumulation of creating unnecessary references at some time takes its toll. But sometimes you have to group items, as in the following example:

(Figure|Map|Graph)s? \d+

This expression matches the same three words as earlier but also their plurals (Maps, Figures, and Graphs). To make sure that the plural “s” applies to all three words, not just to Graph, you need to group the singular forms of the three words and suffix the “s” to that group, as in the example expression. But this group creates a reference. To avoid that, use a so-called non-capturing group:

(?:Figure|Map|Graph)s? \d+

Because of the simple addition of ?: InDesign doesn’t create references for our group. I’ve seen enormous speed gains in GREP expressions simply by avoiding the use of capturing parentheses as much as possible.

2. Use + when you can, * only if you have to

The two wildcards + and * are well known but not always used correctly. They look similar, and * can often be used instead of +, but * usually does more than is necessary. The difference between the two is that, because * matches ‘nothing’, your GREP often ends up doing unnecessary work. For example, to remove spaces at the end of paragraphs, this expression can be used:


(\x20 is the Unicode value of the space, $ stands for ‘end of paragraph’.) This GREP expression does the job: it deletes all spaces at the end of every paragraph. But it does too much: it matches *every* paragraph-end because you tell InDesign to look for paragraphs that have zero or more spaces at the end, which is a waste. A simple change:


tells InDesign to match only paragraphs that have one or more spaces at the end. In long texts the difference is significant.

3. Mind your based-on styles

When you base paragraph style B on paragraph style A, and if paragraph style A defines some GREP styles, then those GREP styles are active in style B too. If you don’t need those GREP styles in paragraph style B, then rather than basing B on A it’s better to duplicate style A as style B and remove the GREP styles from B.

Editor’s Note: If you liked this article, you’ll love Peter Kahrel’s feature article in issue #59 of InDesign Magazine, “Getting a Grip on GREP.”

Peter Kahrel is a script developer at Typefi Systems. He is the author of several eBooks on InDesign and scripting, including GREP in InDesign, published by O’Reilly Media. He also wrote the feature article on “Getting a Grip on GREP” in InDesign Magazine Issue 59.

Related Articles

4 Comments on “Speed Up Your GREP Styles

  1. Hi Peter – thanks for the tips, they are very helpful!

    I am actually trying to troubleshoot a sluggish file right now and there are a few GREPs built into stylesheets – so i will test these out, however, i am also using a Typefi Autofit box in a few places – is it possible that that can cause sluggishness?

    Also, i don’t know if this is the right forum for this, but would you know if/why a group containing a Typefi Autofit box will not work as an anchored object? It just doesn’t paste in…


    • Zalman,

      Frames that use some Typefi Autofit settings will naturally process a bit slower than frames without Autofit settings. The same goes for frames in which you use InDesign’s Auto-size feature (this is comparable to Typefi’s Autofit, but available only from CS6 and has a smaller feature set).

      As to the group containing a Typefi Autofit box not working as an anchored object, I’m not sure what the problem is. A simple experiment works ok for me.
      Maybe this is not the place to go into that here — you’re better off posting in Typefi’s Autofit support page at


  2. Peter,

    “Figure|Map|Graph \d+” without the parentheses does not work in the files I’ve tested it in. Instead, it sees the options as “Figure”, “Map”, and “Graph+space+digit”, and any formatting to Figure and Map are applied only to the words and not to the number(s). However, if the last construct you show accomplishes the same thing, I don’t mind the extra code. Good to know.

    • David,

      Ah, yes, I hadn’t noticed earlier that the digit wasn’t captured (well, also because I made a last-minute change). My formulation without the parentheses indeed matches ‘Figure’, ‘Map’, and ‘Graph + number’. But the last construct works as stated.

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>