This is the fourth and final blog-posts in a series of posts on lookaheads and lookbehinds in GREP, written after speaking at the Perth InDesign User Group. My speaker notes are also available: What is GREP? (PDF download) (2.4Mb). In the previous three posts I briefly introduced GREP, and we took a look at Positive Lookahead, Negative Lookahead and Positive Lookbehind. In this post we’ll review the Negative Lookbehind.
The Negative Lookbehind
To understand what a negative lookbehind is, imagine yourself standing on top of that particular bit of text you want to change. Put your back to the start of the sentence you are in and look behind you… what are you expecting not to find there?
In the example below you are looking at finding a number when it does not have a dollar sign behind it.
Step into the sentence and onto the number(s) you are looking for.
- Ask yourself a question: If I look behind this number, do I spot a dollar sign?
- A negative lookbehind has a negative answer to this question.
Let’s apply a GREP style that highlights any number (digit) in pink first. To showcase how the GREP style applies I’ve created a character style that applies pink colour to the text.
- In the GREP style dialog, click New GREP style.
- Select the character style from the Apply Style menu
- In the To Text area type and from the Special Characters for Search menu, choose Wildcard and select Any Digit.
The To Text area now displays: \d
Enabling Preview will allow you to see how the character style is applied as you work on the GREP style.
- Next insert the cursor in front of
\d
. - From the Special Characters for Search menu, choose Match and select Negative Lookbehind.
The To Text area now displays: (?<!)\d
. Next we need to insert the text that you do not want to locate behind the number ((?<!)\d
) that you’re imagining yourself standing on.
- Insert your cursor before the closing bracket.
- Type \$
Because the ‘$’-symbol has its own GREP meaning, we can’t just type in ‘$’. Any literal character that has its own GREP meaning, must be ‘escaped’ by preceding it with a ‘\’.
The To Text area now displays: (?<!\$)\d
.
We have completed our Negative Lookbehind at this stage.
Although the previous example gives a good usage of the Negative Lookbehind, there are scenarios where you’d need to ‘GREP’ a little further 🙂 For example what happens of there are double digit numbers involved?
The thing to do at this stage is to further isolate the ‘pattern’ of text… We need to find those numbers not preceded by a $-sign, another criteria we have is that those numbers appear at a word boundary… Words consist of alphanumerical (a-z, A-Z, 0-9 and _) character.
The first thing we’ll change in the above GREP is the fact that we are not just looking for ‘single digit’ numbers but also multiple digit numbers…
We originally inserted a Wildcard > Any Digit \d
in the To Text area. To expand this to the GREP picking up any number (single, double or any other number of digits):
- Insert your cursor behind the \d in the To Text area
- From the Special Characters for Search menu, choose Repeat and select One or More times.
Next to isolate the GREP further we refine it to look for numbers not preceded by a dollar sign from the word boundary (or start of word).
- Insert your cursor in front of the opening bracket ‘(‘ in the To Text area
- From the Special Characters for Search menu, choose Locations and select Beginning of Word.
The To Text area now displays: \<(?<!\$)\d
. And the problem of the multiple digit numbers is solved.
You’ve hopefully learned how the various lookaheads and lookbehinds in GREP work.
Going forward, try and combine the lookahead/behinds. If you have a cool combination you’d like to share with the world. Feel free to add a comment at the bottom of the posts.
Your extremely clear explanations and simple examples of positive and negative lookbehind are the best I’ve every seen. Thank you.
wow, thanks for the kind words Robby.
What if we have more than one sign, say dollar and euro?
Great!!
Very useful!!
Fantástico.
Mil gracias.
bedankt Niels 🙂
Hoi Cari,
weer een mooi artikel over grep. 🙂
Niels
Hi Cari,
I really like the illustrations and of course the article 😉
Loic
ha, thanks Loic 🙂
\G\R\E\A\T\!
😉
ha! thanks JRB 🙂