Overview of Advanced GREP
Advanced GREP is a high-performance engine for applying custom formatting to text that matches a GREP expression. Highly configurable and significantly faster than using GREP Styles, Advanced GREP is the ideal tool for those who want to quickly find, correct and/or format text.
The Advanced GREP script executes expressions found in a configuration table in the InDesign template. Using data from the table, the script applies GREP changes and/or applies styles to text matched by GREP expressions in the same way you would using InDesign’s Find/Change dialog. The script provides an interface to that dialog, and whatever can be done interactively in InDesign can be done with the module as well. In addition, the script can be used instead of GREP styles.
What is GREP?
The acronym means “global regular expression print” and is from a UNIX command introduced in 1973. A regular expression is a pattern that can be matched in your document. So GREP, as used in InDesign, is a very powerful way to find and replace content.
There is extensive information about using GREP with InDesign online. Here's a good roundup:
You can also use InDesign Find/Change to try out GREP expressions. The @ button next to the Find what: and Change to: fields contains all the GREP wildcard and other special characters available. You can also enter regular text. Once you have determined the correct expressions, you can copy/paste them into your GREP table.
How to configure Advanced GREP in a workflow
Advanced GREP is defined in a table in the InDesign template and is implemented by an Event Script. You can find the table on an Advanced GREP master page in the template. If your template does not have the configuration table, see Adding a Configuration Table.
The Advanced GREP script is a SpillEnd event. Your workflow should be configured to run SpillEnd scripts, and the script should be placed in the SpillEnd folder on the InDesign server.
Filling in the table
The GREP Configuration table has 9 columns.
Find what text: Enter a GREP expression the same way you would enter it in InDesign’s Find/Change dialog.
Change to text: Enter a replacement expression, again the same way you would enter it in InDesign’s Find/Change dialog.
Find what par style/Find what char style/Change to par style/Change to char style: Optionally enter the names of paragraph and character styles. Give the names as they appear in the style panels. More than one style name can be entered in each cell. Style groups are separated by pipes (“|”). For instance, captions|type denotes character style type in character style group captions. If a style cannot be found, the row is ignored (see next point).
Status: When a job is run, if the four specified styles exist in the document, ok is printed in the Status column. If any of the styles can’t be found, a warning is printed in the job log that tells you which style could not be found. You will only see "ok" in a completed job file, not your template.
Local Overrides: If Change to char style contains the name of a character style and you place an X in Local Overrides, then the style’s formatting is applied as local overrides.
Description: Can be used to add a description of the GREP expression. This column is not used by the script.
Row 1: Find the word Figure or Table, followed by a space, followed by any digit, and replace with the found text, followed by a nonbreaking space, followed by the same digit.
(Figure|Table) means match the text "Figure" or "Table". The pipe | is the "or" operator. The parentheses means "remember what was found."
The space is just a space character.
(\d) means find any digit. \d is the symbol for "any digit" in InDesign GREP. Again, the parentheses means "remember what was found."
$1 means "insert what was found from the first set of parentheses in the Find what text.
~S is the symbol for nonbreaking space.
$2 means "insert what was found in the second set of parentheses in the Find what text.
Because the Find what/Change to columns have no specific styles in them, this GREP will work on every paragraph/character style.
Row 2: Apply the character style smallcaps to two or more consecutive capitals. This could also be done using a GREP style.
Row 3: A text replacement limited to text in the paragraph styles listed under Find what par style. Find section numbers (strings of digits and dots at the beginning of paragraphs) followed by a tab, and replace that tab with an en-space and an indent-to-here. The search is limited to text styled with paragraph styles Heading 1, Heading 2, or Heading 3 in style group Heads.
Rows 4 and 5: This pair of expressions prevents double-hyphenation of compound words by applying the "nobreak" character style as a local override to the 9 characters on either side of a hyphen, em-dash or en-dash. The "nobreak" character style is configured to just apply no break, with no other formatting changes.
Option: Include or exclude hidden layers
To include hidden layers in the GREP search and/or replacement, enter any character in the yellow box above the table. That box is a normal text frame named Include hidden layers on the Layers panel. If the frame is not present or if it is present but is empty, hidden layers are excluded.
Advanced GREP module or GREP styles?
Some considerations for choosing an approach:
- GREP styles cannot be used for replacing text; the module can.
- The Advanced GREP module cannot apply more than one character style to the same text range. InDesign’s GREP styles, on the other hand, can do that.
- Long documents with many InDesign GREP Styles can have performance issues. By using the module, the text is styled explicitly and InDesign does not need to constantly evaluate all the GREP style expressions while you are working on the document.
- Create a new master page in your InDesign template. Name it ‘Advanced GREP’ with whatever prefix your template uses (Z, X, or XC usually).
- Download AdvancedGREPTable.idms (attached below).
- Go to File and then Place.
- Browse to the AdvancedGREPTable.idms.
- Paste the snippet on the ‘Advanced GREP’ master page. An ‘Advanced GREP’ layer will be created.