Join Michael Murphy for an in-depth discussion in this video Escaping out metacharacters, part of Learning GREP with InDesign.
A small handful of metacharacters are not preceded by a backslash or a tilde. They're made up of only one character. These include the period, the question mark, plus sign, and asterisk among others. If you need to describe any of these actual characters with GREP, it requires what's known as escaping out the metacharacter. Let's take a look at what that means. I'm going to switch to the Text tool, click inside this body copy. I'm going to open up the Find/Change dialog. I'll choose Edit>Find/ Change, switch to the GREP tab.
If I'm looking for a period in this document and I type a period in the Find what field in the GREP tab, choose Find, take a look at what it found. I'm going to zoom in here so we can get a closer look. It's actually highlighted a space. If I choose Find Next, it's highlighted the next letter, and then the next letter, the next letter, and now a space. So it's finding any character, which is exactly what a period means in GREP. It's the wild card for any character.
In order to find an actual period, what you need to do is put your cursor before that period here and type a backslash. This is what's known as escaping it out. The backslash changes the meaning of that period from any character to a literal period. So if I click Find, I find a period, and the next period, and the next one. I don't have those extraneous matches that I did not want. This is true of all of the single- character metacharacters in GREP.
You must escape them out with a backslash if you're trying to find the literal character. I'm going to clear this out here. If, for example, you were looking for an opening parenthesis, you would find an opening parenthesis under the Symbols submenu, opening parenthesis character. When that's inserted, notice it's got the backslash before it already. The same is true if you were to choose Close Parenthesis Character. But not every character that you need to escape out is in here. The Plus sign, the Question Mark, the Asterisk and some others also require escaping out.
There is no area here in the Find/ Change dialog that's going to specifically tell you every metacharacter that needs to be escaped out. So you'll have to become more familiar with them as we work. Throughout this course, I'll point out any time that a special character needs to be escaped out. Any single-character metacharacter you encounter as you work with GREP, and we'll see a lot of them throughout this course, must be escaped out if you need to find it literally. Not properly escaping out these metacharacters, when necessary, will cause any GREP expression to either produce unexpected results or fail altogether.
- Using metacharacters, the building blocks of GREP
- Describing text that may not exist with zero operators
- Applying multiple character styles to the same text with GREP styles
- Eliminating orphaned words at the ends of paragraphs
- Preserving and recalling subexpressions
- Customizing a GREP-based text cleanup script for long documents
Skill Level Intermediate
Q: In the “Dynamically fixing orphaned words with GREP” tutorial the author uses the term:
In an earlier course the author described the + (one or more) modifier as unusable in a lookbehind or lookahead i.e. (?<=.+). What's the difference here?
A: The limitation mentioned in an earlier movie referred only to positive lookbehind and negative lookbehind. I was able to use the one or more times (+) metacharacter in the positive lookahead portion of the expression because that limitation doesn't affect either positive or negative lookahead. It's only when looking backward that GREP ignores the repeat metacharacters.