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

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

text sample, with numbers not preceded by dollar sign are underlined in green, numbers preceded by dollar sign in red.

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

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

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 in front of \d.
  • From the Special Characters for Search menu, choose Match and select Negative Lookbehind.

GREP style dialog

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 ‘\’.

GREP style dialog

The To Text area now displays: (?<!\$)\d.

We have completed our Negative Lookbehind at this stage.

GREP style dialog, with finished GREP and text example


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?

GREP example with double digits displaying issues

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.

GREP example with double digits displaying issues

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.

GREP example with double digits displaying issues

The To Text area now displays: \<(?<!\$)\d. And the problem of the multiple digit numbers is solved.

GREP example with double digits displaying issues

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.

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 [...]
  • Positive Lookbehind – GREP for Designers This is the third 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 two posts I briefly introduced GREP, and we took a look at Positive Lookahead and Negative Lookahead. In this post [...]

11 Comments

  1. Robby Robby
    April 21, 2015    

    Your extremely clear explanations and simple examples of positive and negative lookbehind are the best I’ve every seen. Thank you.

  2. Shakeel Shakeel
    May 14, 2013    

    What if we have more than one sign, say dollar and euro?

  3. Marco Marco
    March 17, 2013    

    Great!!
    Very useful!!

  4. Camilo Camilo
    March 11, 2013    

    Fantástico.

    Mil gracias.

  5. March 8, 2013    

    bedankt Niels 🙂

  6. niels niels
    March 7, 2013    

    Hoi Cari,

    weer een mooi artikel over grep. 🙂

    Niels

  7. March 6, 2013    

    Hi Cari,

    I really like the illustrations and of course the article 😉

    Loic

  8. JRB JRB
    March 5, 2013    

    \G\R\E\A\T\!
    😉

Leave a Reply

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