is now part of CreativePro.com!

Speed Up Your GREP Styles

11

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*$

(\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:

\x20+$

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 the author of several eBooks on InDesign and scripting, including GREP in InDesign, published by CreativePro Network. He also wrote the feature article on "Getting a Grip on GREP" in InDesign Magazine Issue 59.
  • Zalman Friedman says:

    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…

    Thanks!

    • Peter Kahrel says:

      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 https://www.typefi.com/typefi-support/autofit-support

      Peter

  • David Popham says:

    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.

    • Peter Kahrel says:

      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.

  • Holly says:

    Thought I might ask here regarding GREP styles. I am trying to create a GREP style to superscripts from references. It is applying to the superscripts to body of text, but all numbers are superscripted. I can’t figure out how to apply “Find single digit before a letter” I want to exclude digits of two or more.It seems to be finding any digit.

    “1Institute of Medical Biochemistry Leopoldo de Meis, 2Division of Neurology/Epilepsy Program, 3Division of Neurosurgery, Clementino Fraga Filho Hospital, and 4School of Pharmacy, Federal University of Rio De Janeiro, Rio de Janeiro 21941-902, Brazil; 5Department of Neurobiology, Northwestern University, Evanston, IL 60208-3520, USA; 6Centre for Neuroscience Studies, Queen’s University, Kingston, ON K7L 3N6, Canada.”

  • Aaron says:

    I’m working on an ID file that I received from a client. I can’t get the GREP styles to inherit from ‘par style 1’ to the ‘par style 2’ that is based on it. Any idea how to troubleshoot this? (Style 2 has no GREP styles in it, so it should just inherit those from style 1…)

    Baffled…

    • Aaron says:

      I should add that reassigning the styled paragraph that I’m testing, to ‘par style 1’, makes it work fine. So the original GREP styles in that style are not in question – only the inheritance of them.

      • Mike Rankin says:

        Are there multiple GREP styles in play? Could there be a conflict where one is being applied after the one you’re not seeing the effect of?

  • Aaron says:

    I thought of that too, Mike. But there are no nested or GREP styles in the 2nd style.
    I do see something possibly suspicious: In the ‘style settings’ overview of the 2nd style (under ‘General’), it says ‘+ nested grep styles: none’. Originally the style did have some of its own GREP styles, which I’d removed. This must be the cause. But how to wipe its slate clean without rebuilding the style? (Many other styles in the doc are based on the same root style #1, and I’d rather not rebuild them all!)

    • Mike Rankin says:

      Ah yes, that’s probably what happened. I think if you re-create the GREP style in the 2nd style so it exactly matches the original, you should be OK. It will act as if it is getting the GREP style information from the first style, as long as it is still based on that style (i.e. it will reflect any changes you make to the GREP style in the first style). At least that’s what happens in a little test I just did on my machine.

  • Aaron says:

    Thanks Mike. I’m sure that would work, but that would mean replicating 3 GREP styles for each of many paragraph styles, which is time-consuming (and the interface doesn’t make it easy to duplicate them, either).

    Seems like a bug; I wonder if anyone else has encountered this.

  • >