This is the second in a series of blog-posts about GREP, written after speaking at the Perth InDesign User Group. My speaker notes are also available: What is GREP? (2.4Mb PDF download). In the previous post GREP was introduced, and we took a look at the Positive Lookahead. In this post we’ll review the Negative Lookahead.
The Negative Lookahead
To understand what a negative lookahead 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 ahead think about what’s not supposed to be in front of it…
In the example below you are looking at finding the word ‘chapter’ when it is not followed by a space and a number. In fact we’re looking at finding the opposite of what we looked for in previous blog-post’s Positive Lookahead.
Step into the sentence and onto the word or text you are looking for (‘chapter’ in this example).
- Ask yourself a question: Is the word ‘chapter’ that I’m standing on followed by a space and number?
- A negative lookahead has a negative answer to this question.
Let’s apply a GREP style that highlights the word ‘chapter’ in pink first. To highlight how GREP works 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 the text you’d like the character style to locate (‘chapter’ in this example).
Enabling Preview will allow you to see how the character style is applied as you work on the GREP style.
- Next insert the cursor after the word ‘chapter’.
- From the Special Characters for Search menu, choose Match and select Negative Lookahead.
The To Text area now displays: chapter(?!)
. Next we need to insert the text that is ahead of the word we’re standing on…
- Insert your cursor before the closing bracket.
- Special Characters for Search menu, choose Wildcards and select Any White Space.
The To Text area now displays: chapter(?!\s)
. Leave the insertion point flashing after the \s
- Insert your cursor before the closing bracket.
- Special Characters for Search menu, choose Wildcards and select Any Digit.
The To Text area now displays: chapter(?!\s\d)
.
It is not necessary – as we are looking for ‘chapter’ – to look for more than a single digit (\d+)
after the space.
We have completed our Negative Lookahead at this stage. The third post in this series covers the Positive Lookbehind.
Leave a Reply