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:

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. 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), clicking it highlights all found instances.

Below the Find button 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).

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

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 GREP codes from a list. Click Class picker to open the list. (The class picker is a separate script; if it's not installed, the button is disabled.) The list contains just the standard wildcards, Unicode properties, the POSIX classes, locations, conditions (Match), and the modifiers. To insert a code into the editor window, double-click it or select it and click OK.

character classes and wildcards

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

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, click 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 click 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, click 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. Click 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

Saving expressions

To save an expression to an InDesign GREP query, go to InDesign's Find/Change window. The GREP expression you used in the GREP editor is in in the 'Find what' field. Save the query as usual.


Version history

30 June 2019: Less is more. The 'Close this window' button wasn't really necessary so that's gone now. Some code optimalisation here and there -- otherwise the script functions the same. The class-picker looked in the wrong folder, that's now synchronised.

10 December 2017: Back to basics. Stripped all the fluff from the script, it's now much leaner.

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

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.

16 October 2011: Clicking 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: Clicking 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.


Useful script?

Consider making a donation. To make a donation, please click 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