A GREP editor

To say that the Find what field in the Find/Change dialog's GREP tab is a tight place is an understatement. It's not very convenient for entering and editing GREP expressions of more than a dozen characters.

While I was experimenting with some dialog scripting issues I drifted into putting together a GREP editor. The result on this page, a GREP "editor" (with some emphasis on the quotes), is therefore more the result of an experiment gotten out of hand than a planned script, as is the case with many scripts on this site.

These are the script's features:

The script's usability depends on the InDesign version you run it in. In CS3 it works very well; CS4 can be a bit of a problem (see the section on troubleshooting at the end of this page); in CS5 and 5.5 it runs very well, almost as good as in CS3; CS6 has further improvement, most noticeably the behaviour of the Enter key. In CC you can't resize the window's type, but otherwise the script works well in CC.

When run, the script shows this window:

GREP editor

I'll first briefly outline the controls in the interface, then I'll illustrate these points in some detail.

The interface

The white area in the middle is the editor window in which you type a GREP expression. At the left of this window are some buttons to handle the contents of the window:

To the right of the edit window are several controls. The first one is the Find button. This button does not behave like InDesign's Find button in that rather than cycling from one found instance to another (if any), pressing it highlights all found instances.

Below the Find button is a row of tool buttons – grep editor options – the familiar options from InDesign's Find/Change dialog. Below this group are some buttons with further options:

The editor is a panel like other InDesign panels, so while it is displayed you can continue working in InDesign. The panel can be resized in the usual way by dragging a corner with the mouse (in Windows you can drag one of the sides as well). Resizing doesn't work very well in CS3 and in CC.

Entering an expression

Take this GREP expression:

(?<=\d)\s(?=Jan(\.|uary)|Feb(\.|ruary)|Mar(\.|ch)|Apr(\.|il)|May|
Jun(\.|e)|Jul(\.|y)|Aug(\.|ust)|Sep(\.|tember)|Oct(\.|ober)|
Nov(\.|ember)|Dec(\.|ember))

Not a thing of great clarity. You can work out after some brain gymnastics that this expression looks for spaces which are preceded by a digit and followed by the name of a month or its abbreviation. It's a nightmare to change anything in an expression like this, but if you format it in the editor, amending the expression is easy:

grep editor 2

You can type as many spaces as you want, wherever you want. In the screenshot you see that comments (everything in (?#...)) can be used to make the text of the GREP expression clearer and that the space bar was used to indent lines. All these spaces have no meaning for the GREP expression once it is transferred to the Find/Change dialog. (You can insert tabs by pressing Ctrl/Cmd+Tab, but I find that they insert far too much space so I simply use the space bar.)

New lines

A problem in versions before CS6. In CS4 and often in CS5x too, inserting a new line can be something of a challenge (CS3 is no problem). In CS3 and CS5 and later you can press Ctrl+Num Enter (Windows) or Return (Mac) to create a new line, not the Enter key. But a fail-safe method is to press grep editor enter new line. From CS6 this is no longer an issue.

Spaces

Since the space character is often used for indentation and tabulation, you must not use it if you want the expression to match a space. Instead, use \x{20}, the Unicode representation of the space. For instance, to look for the word "Table" followed by a space and a digit, use Table\x{20}\d, not Table \d. Alternatively, use \s, which captures all types of white space, including tabs and paragraph marks.

Entering GREP codes

Like in the Find/Change dialog, you can pick a number of GREP codes from a list. And as in the Find/Change dialog, you click grep editor pick tool to open the list. The list contains just the standard wildcards, Unicode properties (visible only in CS4 and later), the POSIX classes, locations, conditions (Match), and the modifiers. To insert a code into the editor window, double-click it or select it and press OK.

character classes and wildcards

To expand a section, double-click the section name (Standard wildcards, POSIX, etc.) or click the + or triangle preceding it. Sections can be collapsed by double-clicking the section name again or by pressing the - or the triangle in front of the name.

Because of a bug in CS3 and CS4, GREP codes are inserted at the correct position (at the insertion point) only in the first line. In the second line the code is inserted one position to the right; in the third line, two positions, etc. To counter this, type a few spaces, enter the GREP code at the beginning of the spaces, insert the code, and remove the excess space (though any excess space is ignored). This problem was fixed in CS5.

Using formatting codes

It's not possible to enter formatting codes as part of the GREP expression. If you want to make the GREP expression match certain formatting, you have to add these in the Find/Change expression. See the next section.

Testing the GREP expression

To test your GREP expression, press Find in the editor's window. Unlike InDesign, but like several editors (including MS Word), the script highlights everything that matches your expression, as shown in the screenshot ([aeiou]{3} means "match three consecutive vowels"):

 

grep editor

The script tells you how many matches it found for your expression in the window's frame after the title.

The Find button is in fact a tool button. While it is pressed down, the script is live in the sense that it matches immediately any change you make to the expression. This is very quick, but if you're working in a big document and your expression has many matches (the GREP expression e can have thousands of matches), you'll notice some lag. To avoid this, disable the live update by pressing Found again (the button's text returns to Find). Finish your expression, then press Find again to enable the live view.

Some background

1. The highlighting is handled by a condition. It's called GREP_editor_highlight. It's created by the script and is applied to your GREP expression's matches, and also removed by the script. If, for whatever reason, after using the script you're stuck with highlighted text applied by the script, open the Conditional Text panel (Window > Type & Tables > Conditional Text) and delete the condition.

2. Any line breaks in the edit window are replaced with (?#) in the Find/Change dialog. (?#) is an empty comment, so to speak, and is used as a placeholder for the line break. After all, you might want to save the GREP expression in a query and later place it in the editor again. When you do transfer the expression from the dialog to the editor later, the script replaces these placeholders with line breaks and displays the expression just as it was when you typed it earlier.

grep editor 3

In addition, the script adds the modifier (?x) at the beginning of the expression if it contains any space – "space" meaning any space, tab, and/or new-line characters. This modifier tells InDesign to ignore all spaces when the GREP is executed. This is the reason why you must not type a space when you want your GREP expression to match a space (use \x{20} or \s instead).

Send to GREP style

To copy a GREP expression to one or more paragraph styles, press Send to GREP style. Your paragraph styles are shown in a list in which you can select one or more styles, and you can select the character style in the dropdown. Press OK to copy the GREP expression to all the selected styles.

Style groups are shown using >, as in the screenshot.

send grep style to paragraph style

Modifying existing expressions

To change an existing expression, for instance one that you've saved in a query, press Load GREP query. This displays all your GREP queries. Select the query and its Find what string will be copied into the editor's window. You can zoom in on an expression by right-clicking it. For details, see the description and screenshots of the GREP-queries overview script. When you load a query, its name is shown in the editor's title bar (as you can see in some of the screenshots).

Saving expressions

To save an expression to an InDesign GREP query, press Save GREP query. In a familiar dialog you'll be asked to provide a name; if the query exists you're asked to replace it.

grep editor save query

Troubleshooting (especially CS4)

Something has gone wrong between CS3 and CS4. In CS3, the editor works virtually as expected. The only thing to remember is that to insert a new line, on Windows you need to press Ctrl+Enter or Ctrl+Num Enter, on the Mac you press Enter (not Return). To insert a tab, press Ctrl/Cmd+Tab. In CS3, Copy and Paste work as expected.

In CS4 things can be tricky. Everything works fine when there's no insertion point and when the Find/Change panel is not displayed. If the panel is on the screen and/or there is an insertion point, you may run into odd keyboard behaviour, but there's usually a solution:

In CS5 things are virtually back to normal. You still can't use the Copy and Paste shortcut keys, but that's not a problem as you can copy and paste using the context menu (right-click in the window).

CS6 is another improvement in that the Enter key can be used normally, so if the editor is used in CS6, the button to insert new lines is no longer needed.


Version history

28 February 2015: Updated the GREP class picker with two newly discovered classes: \K (a variable-length lookbehind) and \R (match any break character -- line, page, frame, etc.). These two were mentioned by Kai Rübsamen in Adobe's scripting forum (see here).

13 January 2014: When copying a GREP expression from the F/C window, the editor's codes weren't changed correctly; fixed. Changed the Find button to a normal button to get around the display problems of toolbuttons in CC. Changed the layout of the class picker.

26 November 2013: Expanded the class inserter (it has now more elaborate help text, especially for the Unicode properties and the modifiers); when you send a GREP expression to a paragraph as a GREP style, you can now also select a character style; in CC the window uses CC icons.

14 June 2012: an old and neglected bug finally fixed. The bug was that once you had pressed the Clear this window button, using the class inserter would clear the editor window's contents (thanks to René Andritsch for spotting this). This is now fixed. The fix led to a general overhaul of the class selector. The only visible aspect is the addition of the category Repeat. In addition, the dialog's font handling is now more streamlined. (InDesign needs to be restarted if you had been using a version of the editor in the current InDesign session.)

17 May 2012: (1) The bug fixed on 3 February came back; fixed for good. (2) The buttons are now embedded in the script. That means that the following images can be removed from the scripts folder:

grep_editor-find.png
grep_editor-opt-footnotes.idrc
grep_editor-opt-hidden-layers.idrc
grep_editor-opt-highlight.png
grep_editor-opt-locked-layers.idrc
grep_editor-opt-locked-stories.idrc
grep_editor-opt-masters.idrc
grep_editor-opt-zoom.idrc
grep_editor-tooltips.idrc
grep_editor_bigger.idrc
grep_editor_highlight.idrc
grep_editor_newline.idrc
grep_editor_pick.idrc
grep_editor_smaller.idrc

8 May 2012: A call to a named font sometimes prevented the wildcard/class selector to appear. Fixed.

23 April 2012: (1) From CS6, the Enter key can finally be made to work as expected in edit panes; applied to the script. (2) The script created a new window every time it was started. Now it checks if there's a window that can be reused. This should fix some awkward memory and keyboard problems.

3 February 2012: Fixed a bug that sometimes prevented the ChangeTo field to be emptied.

16 October 2011: Pressing Find now highlights all matches in the active document rather than cycling through individual found items. The highlights are updated instantly when you edit the GREP expression.

26 October 2010: Pressing Load GREP query now fires up a separate GREP query manager (formerly known as 'GREP overview') which offers some more possibilities than the version that was built into the editor.

28 July 2010: Reorganised the window; added possibility to save a GREP query.

15 July 2010: Added dialog to load expressions from GREP queries.

1 June 2010: Changed handling of the two icons.



Useful script?

Consider making a donation. To make a donation, please press the button below. This is Paypal's payment system; you don't need a Paypal account to use it: you can use several types/brands of credit and debit card.

Peter Kahrel's paypal account

Installation

Click the link below, which downloads a zip file that contains the script. Unzip that file in your script folder. Done.


Click to to download script

Back to GREP page

Back to main script page

Installing and running scripts

Questions, comments? Get in touch