Dealing with long GREP expressions

Recently, I needed to tweak a rather long GREP expression. I hadn't seen it for a while, and when the expression greeted me as shown in the screenshot my heart sank; I could barely make out what was going on.

GREP Find/Change screenshot

InDesign offers two standard GREP features to make GREP expressions a bit clearer: (?x) lets you use spaces in a GREP and with (?# ...) you can add comments in an expression. Though these two features may come in handy occasionaly, most of the time they aren't much help: in fact, they often make GREPs more complicated than they already are.

I decided I needed something to make editing long GREP expressions less awkward. What I wanted was something with more space and better display. The result is a system which I'll illustrate with a short example.

InDesign the GREP editor

Take this GREP expression: \b([\d()]+)[-~~]([\d()]+)\b. Though it's not too complicated, it doesn't exactly read very easily. But when you present that same GREP like this:

GREP formatter screenshot

that is, with groups on separate lines, indents to show structure, and colours to highlight different language elements, the expression is much clearer. Comments further improve the GREP so that you can understand later what the GREP actually does or is supposed to do.

As you can see in the screenshot, the GREP was typed in an InDesign document. It is convenient to use InDesign for this because we can structure the text with tabs and returns and add colour to comments and GREP elements -- we all know InDesign well enough to make use of its editing capabilities. The document uses GREP styles to add the colours (square brackets blue, parentheses green, wildcards orange, literals black). The document, grep_format.indd, is included in the download, see below. It contains all the necessary styles. The system outlined here works in CS3 and later (though in CS3, because it has no GREP styles, slightly differently).

Before continuing, install the script and the InDesign document:

Preparation

1. Download the file linked at the foot of this page. It's a ZIP file that contains two files: the script grep_edit.jsx and an InDesign document, grep_format.inx. Copy both files to your scripts folder. You can find that folder as explained here.

2. Open grep_format.inx in your version of InDesign, then save it as an InDesign document in your script folder and close it.

Use

To use the GREP editor, run the grep_edit.jsx script. It opens the grep_format document and places this panel on your screen:

GREP editor panel CS4

When you run the script in CS3, the panel has one extra option:

GREP editor panel CS3

The additional option (Highlight syntax) will be illustrated later. We'll begin with opening an existing GREP expression.

Transferring a GREP from the Find/Change dialog to the document

Can't we just copy a GREP expression and paste it into the document? Well, if we did that, all the escaped characters are pasted as their literals (\s as a normal space, ~] as a quote, etc.). So we can't use the clipboard to copy and paste the expression. We'll therefore use the script to transfer the expression.

Open the Find/Change dialog and select one of the GREP queries that shipped with InDesign. We'll use Phone Number Conversion as an example. It looks like this in the Find/Change dialog:

GREP editor F/C dialog

Not exactly easy to read, this one. To transfer this expression to the document, draw a text frame and select it. Press Fetch from F/C dialog to transfer the GREP expression from the dialog to the selected text frame. The result is something like this:

GREP editor

As you see, both the Find what and the Change to expressions are transferred. In CS4, syntax highlighting is applied automatically (as you can see from the colours applied to different parts of the expression). When you run the script in CS4, you need to press Highlight syntax in the script's panel to get that effect.

You can now make the GREP expression more readable by breaking it up and adding comments. This could be the result:

GREP editor

Note: You may experience some odd keyboard behaviour. This happens after you've pressed a button in the script's panel: when you do, the panel captures most keyboard focus. To remedy this, simply activate the Find/Change dialog (or any other dialog).

Transferring the formatted GREP to the Find/Change dialog

To transfer a GREP expression to the Find/Change dialog, make sure that the paragraph style find_what is applied to all paragraphs making up the Find what expression, and, if you have a Change to expression as well, that the paragraph style change_to is applied to the Change to expression.

Since we have a formatted expression in our document from the previous step, we'll transfer it. But to see that it really works, first clear the Find/Change dialog by pressing Clear F/C dialog in the script's panel.

To transfer the expression to the Find/Change dialog, press Send to F/C dialog. The result is shown in the dialog straight away.


Download ZIP file (18 Kb)

Back to GREP page

Back to main script page

Installing and running scripts

Questions, comments? Get in touch