Fan of creative technology, elearning, instructional design and a little geeky

Negative Lookahead – GREP for Designers

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.

text sample, with chapter preceding a number underlined in red when chapter not preceded by number underlined in green.

visual example of man standing on word 'chapter', looking ahead.

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).

GREP style dialog

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.

GREP style dialog

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.

GREP style dialog

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.

GREP style dialog

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.

GREP style dialog, with finished GREP and text example

Similar posts
  • Adobe InDesign and XML: A Reference Guide The following is a reference guide I have compiled over time for those of you who are looking at doing a little more with XML in InDesign (originally published Feb. 2011, last updated Feb 2018). If you have any extra information you’d like to see added to this guide, feel free to message me. Mapping XML tags to InDesign [...]
  • How to change the colour of bullet points in InDesig... A few weeks ago I recorded a short quick tip tutorial. InDesign’s Control panel, gives users a quick and easy way to format text as a bulleted list. Highlight the text, and click the Bulleted List button in the Paragraph Formatting Controls mode for the Control panel. This applies a universal bullet character as the [...]
  • Creating a pop-up window in a PDF with Adobe InDesig... In the following YouTube tutorial, we’ll create an interactive PDF from Adobe InDesign, in which we click on a button, which in turn opens up a simple pop-up window containing a close-box. When the close-box is clicked the pop-up window disappears [...]
  • InDesign: Facing pages with odd-numbered left pages Back in 2004 I wrote a tip for InDesign CS on how you could have the first page of your document be a left page AND have it start with page number 1. It’s hard to believe this is almost a decade ago… But a recent question on Facebook, made me think I should really [...]
  • Negative Lookbehind – GREP for Designers 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 [...]

No Comments Yet

Leave a Reply

Your email address will not be published. Required fields are marked *