GREP of the Month: This or That
The vertical bar helps when you need options in your GREP searches.
This article appears in Issue 81 of InDesign Magazine.
Sometimes you need to find several different strings of text in your document. If you know the specific strings, you could always do individual searches, using the Text tab of InDesign’s Find/Change dialog box. But you can save time by writing a single GREP expression that covers each string you’re looking for. Plus, using a “this or that” expression lets you build the search into a paragraph style. The magic “either/or” symbol in GREP is the vertical slash |, or pipe, typically found by pressing Shift+backslash.
In a GREP string, the expression will look for everything on either side of the pipe. For instance, the expression candy apple|banana finds candy apple or banana (but not candy banana). An example of this in a find/change search would be to find a list of product names Anvils|Dynamite|Rocket Skates and change it to $0~d, which means add the trademark symbol ~d to the found text $0. Since GREP wants to take everything to the sides of the pipe, you often need to isolate the list of strings you’re looking for. You do this by creating a subexpression, which involves encasing the list in parentheses. If I searched on ACME Anvils|Dynamite|Rocket Skates, I’d get “ACME Anvils” or “Dynamite” or “Rocket Skates”. However, if I search for ACME (Anvils|Dynamite|Rocket Skates), I’ll find ACME Anvils, ACME Dynamite, or ACME Rocket Skates. You can also use a subexpression to isolate parts of a word, for instance to find alternate spellings. Searching adapt(o|e)r will find adaptor or adapter. You could use adaptor|adapter, but the former is quicker and cleaner. One thing to remember is that GREP works in a logical fashion when applying GREP Styles. If you search for cat|category, InDesign won’t find “category” because
it finds “cat” in the word first. So put the longest word first in the list, like category|cat. —Erica Gamet
Commenting is easier and faster when you're logged in!
Recommended for you
Making Catalogs that Work (with Less Work)
Transform your catalog designs from ho-hum to high performing with these tips.
Designing Book Covers in InDesign
Learn how the key to designing a book jacket lies in the execution of the InDesi...
InDesigner: LensWork
A look at LensWork, a magazine devoted to black and white photography, is produc...
