Speed Up Your GREP Styles

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.”

Bookmark
Please login to bookmark Close

This article was last modified on December 30, 2021

Comments (11)

Leave a Reply

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

Loading comments...