Advanced GREP Styles 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 Styles is the ideal tool for those who want to quickly find, correct and/or format text.
The Advanced GREP Styles event 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?
GREP is an acronym for “Globally search for the Regular Expression and Print”. It started as a UNIX command line utility that searched input files for patterns, but many applications, including InDesign (since InDesign CS3), have adopted GREP as a powerful tool to find patterns in text.
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. Use the @ button next to the Find what: and Change to: fields as a cheatsheet for the special characters unique to InDesign. You can also enter regular expressions as text. Once you have determined the correct pattern, you can copy/paste them into your GREP table.
How to configure Advanced GREP Styles in a workflow
Advanced GREP Styles are defined in a table in the InDesign template and applied 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 below.
Your workflow should be configured to run this script at the SpillEnd event.
Filling in the table
The configuration table for Advanced GREP Styles 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 …/Change to … style: Optionally enter the names of paragraph and character styles. Give the names as they appear in the style panels. Put each style name on its own line in the table 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 Status below).
Find what par style: To target multiple paragraph styles, either list the style names or use a pattern. To list style names, place each name on a new line in the cell (or separate the names with a tab). A pattern matches from the beginning of style names. For example, the pattern List... (List followed by three dots) matches all paragraph styles whose names start with List.
The two above options can be combined in a cell, so you could have a cell in the Find what par style column like this:
which would create queries for the single Body Text paragraph style and for every paragraph style whose name starts with List.
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 previously found 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 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 Styles or InDesign's GREP styles?
Some considerations for choosing between the Advanced GREP Styles event script or InDesign's GREP styles:
- Advanced GREP styles can be used to replace text whereas InDesign's GREP styles cannot.
- InDesign's GREP styles can apply more than one character styles to the same text range; the Advanced GREP Styles event script cannot.
- Advanced GREP Styles doesn't suffer from degraded performance like InDesign's GREP styles does for long documents or documents with many GREP styles.
- Download AdvancedGREPTable.idms.
- Create a new master page in your InDesign template. Name it ‘Advanced GREP’ with whatever prefix your template uses (Z, X, or XC usually).
- Choose File → Place and select the AdvancedGREPTable.idms you downloaded previously.
- Place the snippet on master page created in step 2. An ‘Advanced GREP’ layer will be created.