Most recent update (All By Hand(TM)): 4-Dec-2018 15:37
... drain you of your sanity,
face The Thing That Should Not Be ...
Metallica, The Thing That Should Not Be
InDesign CS4/CS5/CS5.5 GREP
GREP is one of the most powerful features in InDesign. It can save lots of time: anything you can do with the regular text search and replace, you can do with GREP, and there are lots more things you can only do with GREP.
But using GREP is hard ... The basics are easy enough to grasp: the concept of wildcards for any character, upper- and lowercase, and digits; start and end of paragraphs; zero-, once- and more-repeats; and even basic inclusion and exclusion groups. Add nested grouping and OR bars, and things tend to get muddy (even though this allows for some fairly advanced tricks). Mention Negative and Positive Lookbehind and Lookahead, and your average InDesigner's eyes will start to glaze over. Throw in Expression Modifiers, Unicode and POSIX classes, and Atomic groups. Then, for good measure, take a look at If/Then/Else constructions ... and even an experienced GREP user will soon loose his grip over his own creations.
Adobe doesn't make it easy to figure out why a certain GREP expression fails to find anything, as this may ultimately be for several reasons:
While I cannot help you on the first point, I can sure make the second easier, and the third point -- validating a GREP expression -- is "only" an exercise in programming acumen. That's where I wrote a script that lets you enter a GREP expression and test it for "correctness".
But enough about me. What's this all about?
What the GREP!?
That's what people are shouting when their GREP doesn't seem to be working, and that's the name of the script. Before continuing with explaining what it does and what it doesn't do, download it here:
whatthegrep-0.1.zip (22KB; v0.1(c), dated 14-Dec-2018)
Unpack the ZIP to a temporary location. Copy the file "WhatTheGrep.jsx" into your Users Script folder; if put into the correct place, it should magically appear inside your Scripts Panel in InDesign. Double-click the script to run.
So ... what the GREP does it do?
This is what it looks like:
On running the script, it opens a dialog where you can enter or paste a GREP expression in. Initially, the last GREP expression you used is automatically shown, but you can edit it at will, or simply delete it and replace with another one.
The Close button does just that -- it closes the dialog. May be handy if you changed your mind about running the script.
The Show Me button creates a new document in your InDesign and writes out a full explanation of each of the codes in the expression, with all of its special characters explained, and all groupings enumerated and indented. This is the part where some expression may suddenly be more comprehensible! If the expression contains an error somewhere, it'll be mentioned in this explanation.
As with all scripted dialogs, the Return key closes the dialog and runs the default action ("Show Me"), and the Escape key closes it without doing anything at all. Fortunately, that's not a problem with GREP -- no need to insert hard returns. You can insert Soft Returns (Ctrl+Return) but beware these will also pop up in the explanation.
At the top of the new document, the original GREP expression is shown. At the bottom, a cleaned-up version is written out, with color codings and super- and subscripts denoting various properties. Any erronous code will be shown in bright red bold italics, so you can immediately see why and where the expression fails.
"What the GREP" understands a fair part of the regular daily commands. There are only a few cases where it reports an error where there is none -- for example, the expression [a-z is perfectly legal in InDesign, yet my script will notice the second opening [ and assume it's an error. However, in general, you are supposed to trust its judgement ...
There are also a few rarely used command sequences that are baffling me, and that may cause the script to either accept a faulty expression (hopefully, very rare), or signal an error where there is none (not quite as rare as I'd hope...). Then again, if you are confident to actually use one of those functions, you'll be able to judge by yourself what your error was, as well as appreciate the amount of work I put into this.
What the GREP only explains Find What expressions. Beginners, beware! "Replace With" expressions are different from this, and you cannot enter a "Replace With" expression into the dialog and expect any meaningful results.
(Some day I just might look into that as well, but not on any short notice!)
InDesign's GREP flavor
Following www.regular-expressions.info, here is a list of GREP features and InDesign's support for these, as I understand them.
There are a few question marks, because I'm not really sure how these functions should to work ... Any comments on this list are highly appreciated!
Note: not all commands listed as "YES" are recognized by What the GREP itself.
Many thanks to Adobe for writing both InDesign and its GREP abilities, and to Jan Goyvaerts of http://www.regular-expressions.info for writing a thorough overview of All Things GREP. A definitive must-read page by page for an in-depth look at GREP!
Thanks to TroyCole for pointing out a Random Difference between CS4 and later versions (suddenly, text mysteriously appeared in [Paper] color in modern versions -- thanks to some programmer at Adobe who switched [Paper] and [Black] swatches around!).
This data is by no means exhaustive and/or authoritive. Any observation of InDesign's GREP is mine. The official reference for InDesign's GREP function can be found at the Adobe Systems Incorporated web site. Errors may have crept into my story, and in that case are most likely exclusively mine.
InDesign CS3, InDesign CS4, CS5, and CS5.5 and ExtendScript are trademarks of Adobe Systems Incorporated.