- GREP is a powerful tool to find patterns in text. You can use GREP in InDesign’s Find/Change dialogue or InDesign’s built-in GREP Styles.
- Advanced GREP is a script module for Typefi 8 that enables targeted GREP formatting, without the performance degradation common to InDesign’s built-in GREP Styles.
- To use the Advanced GREP module, you’ll need to:
What is GREP?
GREP is an acronym for ‘Global Regular Expression Printer’. GREP started as a UNIX command-line utility that searches input files for patterns. But many applications, including Adobe InDesign, have adopted GREP as a powerful tool to find patterns in text.
TIP There is extensive information about using GREP with InDesign online. Here’s a good roundup: InDesign Secrets GREP Resources page
Imagine a history book with a lot of text and dates. In this book, all the dates are four-digit years, formatted as YYYY.
You can use GREP to instantly find and edit all these dates if you decide you want to:
- Add bold formatting to all dates (1800)
- Add the text ‘AD’ after all dates (1800 AD)
- Change the dates (instead of 1800, 1801, 1802, you want the dates to be 1700, 1701, 1702)
Find and change in InDesign
InDesign’s Find/Change dialogue (Edit → Find/Change) comes in handy as your document goes through revisions. You can use it to:
- Find and change text: Search for and change specific occurrences of characters, words, or groups of words.
- Find and change formatted text: Search for and change specific occurrences of formatted text.
- Search using GREP expressions: Search for alphanumeric strings and patterns using GREP. The alphanumeric strings and patterns can be replaced or formatted, or both.
- Find and change text with queries: Search for and change specific occurrences of characters, words, groups of words, or formatted text using queries. A query is a defined find-and-change operation.
The Find/Change dialogue affects character formatting by changing either the applied paragraph style, the applied character style, or the actual character formatting through local overrides. Find/Change cannot apply more than one character style.
To make a change, you must manually click Find Next, Change, Change All, or Change/Find.
GREP Styles in InDesign
GREP Styles applies one or more character styles to text that match the GREP expression you specify.
For example, suppose you want to apply a character style to all the email addresses in a document. When you create a GREP Style, you select the character style and specify the GREP expression. All paragraph text that matches the GREP expression is formatted with the character style.
NOTE GREP Styles only applies formatting changes; it does not change the content.
Changes applied with GREP Styles are ‘live’. Anytime you enter new text, InDesign checks the document for matching GREP Styles. If a match is found, the text is formatted with the corresponding character style. If you are working on a long document or a document with many GREP styles, these live changes may cause performance degradation issues.
TIP David Blatner provides real-world examples of GREP styles at 5 Cool Things You Can Do with GREP Styles. And, Cari Jansen provides a four-part series about GREP styles that begins at Introducing GREP Styles.
Find and change using the Advanced GREP module
The Advanced GREP module enables targeted GREP formatting without the manual work of the Find/Change dialogue or the performance degradation of GREP Styles.
Whatever you can do using the Find/Change dialogue, you can also do with Advanced GREP. And, you can use Advanced GREP instead of InDesign’s GREP Styles.
Advanced GREP is significantly faster than InDesign’s GREP Styles, especially when working on long documents or documents with many GREP styles. This is because GREP Styles are live. Every time a text is recomposed, GREP Styles execute. In contrast, the Advanced GREP module applies styles (or makes text changes) only once per story.
Advanced GREP uses an event script called ApplyGrep that automates the running of queries. A configuration table in the Typefi-ready template (.indd) stores these queries. Then, Advanced GREP uses the queries in the configuration table to apply GREP changes in the same way you would using InDesign’s Find/Change dialogue.
NOTE The ApplyGrep script runs at the Spill end event. At Spill end, Typefi has read a story from the Content XML, formatted it, and placed it in a text frame. Typefi Field Instances were resolved through a different process that isn’t part of a story, so the text is never seen by ApplyGrep. If you want to target Typefi Fields, contact your Solutions Consultant for a custom script.
Configure the Advanced GREP module in a Typefi workflow
To configure the Advanced GREP module, you need to:
- Add a configuration table in the Typefi-ready template (.indd).
- Fill in the configuration table.
- Add the ApplyGrep script (if you don’t have access to the script, contact your Solutions Consultant) at the Spill end event in the Create InDesign document or Create InDesign book workflow action.
1. Add a configuration table
- Download AdvancedGREPTable.idms (available at the bottom of this article).
- Create a new master page in your InDesign template. Name it Advanced GREP with whatever prefix your template uses to indicate a configuration master page (usually Z, X, or XC). TIP GREP tables can be quite large, so you may want to make your master page larger than your regular document pages.
- Choose File → Place and select the AdvancedGREPTable.idms you downloaded in step 1.
- Place the snippet on the master page created in step 2. The snippet adds three new frames. One is named GREP, one is named Include hidden layers, and one frame is unnamed.
2. Fill in the table
The configuration table includes one checkbox to include hidden layers and objects and nine columns:
Include hidden layers and object
To include hidden layers in the GREP search 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.
Find what text
Enter a GREP expression just like you would enter it in InDesign’s Find/Change dialogue.
Change to text
Enter a replacement expression, again like you would enter it in InDesign’s Find/Change dialogue.
If you only want to change the text’s paragraph or character style, leave this cell blank.
Find what par style
You can target a single paragraph style name or multiple style names. 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. For example:
And, several patterns are available:
|Literal||The literal name. List matches style names that are List.|
|Three unspaced trailing dots||Matches all style names that start with the name. List... matches all paragraph styles names that start with List.|
|Three unspaced leading dots||Matches all style names that end in the name. ...List matches all paragraph style names that end in List.|
|Three unspaced leading and trailing dots||Matches all style names that include the name. ...List… matches all paragraph style names that include List.|
|Prefixed @||A paragraph style group prefixed with @ includes all paragraph styles in that group. @Titles matches all paragraph styles in the style group Titles.|
You can also combine lists and patterns in a single cell. For example:
|The associated GREP expression applies to all paragraph styles whose name is Body Text, start with List, end in FM, contain Continue, or are in the Table style group.|
If a style cannot be found, the row is ignored, and any missing styles are logged in the action’s log file.
To target the whole text, leave Find what par style empty.
Find what char style, Change to par style, Change to char style
Use these columns to 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.
TIP If your template uses style groups, separate them by pipes (|). For example, captions|type denotes character style type in character style group captions.
If a style cannot be found, the row is ignored, and any missing styles are logged in the action’s log file.
InDesign can’t apply more than one character style to a text range. So to apply some formatting to text which could already have a character style applied, you must apply a local style override.
To apply a local override, place an X in the Local Overrides column. If Change to char style contains the name of a character style, then the style’s formatting is applied as a local override.
You can use this column to add an optional description of the GREP expression.
Example configuration table
NOTE The AdvancedGrep script processes the GREP expressions in the order they appear in the table. It’s important that later expressions don’t mess up earlier ones, or earlier ones don’t change the conditions that later ones are looking for.
Include hidden layers and objects
Hidden layers and objects are included.
Apply the character style smallcaps to two or more consecutive capitals. Since Change to text is blank, this GREP expression only changes the style and leaves the text alone.
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 3 and 4
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 as a local override, with no other formatting changes.
3. Add the AdvancedGrep script to your workflow
Make sure you have access to the ApplyGrep script. (If you don’t have it, contact your Solutions Consultant). You can save ApplyGrep in the Typefi Server filestore or on your local computer.
To add the AdvancedGrep script to your workflow:
- Sign in to your Typefi Server and navigate to the relevant workflow.
- Within Create InDesign document or Create InDesign book, click the Event scripts tab.
- Locate Spill end and click the Choose Files or Folders icon.
- Select the ApplyGrep script from the Typefi Server filestore. Alternatively, add the script from your local computer: click Browse in the Choose Files or Folder dialogue. When you select a script from your local computer, it is automatically added to the Typefi Server filestore.
- Run the workflow.
If you have any questions about the Advanced GREP module, please contact us. We’re here to help.