How to Find Literal Instances of GREP Metacharacters in InDesign
Certain characters perform special functions in GREP. So how do you search for the actual uses of those characters?
This article appears in Issue 78 of InDesign Magazine.
Some characters, known as metacharacters, have special meanings and functions in GREP searches. For instance, a period (.) will find any character—letters, digits, spaces, tabs—anything except a paragraph return. Parentheses are used to group search strings together: (\u|\d) creates a group to find any uppercase letter or any digit surrounded by spaces. The pipe character/vertical line (|) indicates “this or that.”
In all, 13 metacharacters have special meanings in GREP expressions: . $ ? ^ * ( ) [ ] { } + |.
But what if you want to search for any of these actual characters in your text? For instance, you might want to find periods at the end of a sentence, not the “any character” code.
That’s where the backslash (\), or “escape,” character saves the day.
For example, I recently wanted to apply a GREP style to the hundreds of figure labels in 16 documents of a book. These were an uppercase letter and a period surrounded by parentheses—(A.), (B.), (C.), etc. Typing (\u.) in the GREP style field doesn’t work. It completely ignored the parentheses, and then it found any uppercase character followed by any other character. So it would match A., B., and C., as well as the An in Anyone, I’ in I’m, and my initials, SC.
To get around this problem, I needed to use the backslash character. Adding the backslash in front of a metacharacter removed the special meaning and made it act like a literal character. So \( finds an open parenthesis, \) found the close parenthesis, and \. found actual periods.
The complete code for my GREP Style:\(\u\.\).
It’s easy to remember the backslash escape character, because you see it so often in GREP. But if you need a visual cue, think of it as a magic wand that you can wave to convert an ordinary character such as u or d into a magical GREP code (\u or \d), or to reverse the spell and turn the thirteen metacharacters into ordinary characters.
Commenting is easier and faster when you're logged in!
Recommended for you
Using the Selection Brush in Photoshop
Learn how to use the Selection Brush, which lets you make complex selections qui...
InDesign Workarounds
A compendium of workarounds, fixes, and stopgaps for every InDesign user
Fixing Widows, Orphans, and Runts in InDesign
Widows, orphans, and runts may have funny names, but their effect on your layout...
