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

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 we’ll review the Positive Lookbehind.

The Positive Lookbehind

To understand what a positive 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 to find there?

In the example below you are looking at finding the letter ‘c’ when it has numbers behind it for example 99c. In fact we’re basically doing what we did for the Positive Lookahead but instead of opposite direction.

text sample, with 'c' that comes after a number underlined in green and other 'c's underlined in red.

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

Step into the sentence and onto the word or text you are looking for  (the ‘c’ in this example).

  • Ask yourself a question: If I look behind the letter ‘c’ that I’m standing on do I see a number?
  • A positive lookbehind has a positive answer to this question.

Let’s apply a GREP style that highlights the letter ‘c’ 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 the text you’d like the character style to locate  (‘c’ 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 in front of the letter ‘c’.
  • From the Special Characters for Search menu, choose Match and select Positive Lookbehind.

GREP style dialog

The To Text area now displays: (?<=)c. Next we need to insert the text that is behind the letter ‘c’ you’re standing on…

  • 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: (?<=\d)c.

It is not necessary – as we are looking for ‘c’ – to look for more than a single digit (\d+) before the letter ‘c’.

We have completed our Positive Lookbehind at this stage.The final post in this series covers the Negative 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 [...]

7 Comments

  1. June 4, 2015    

    Congratulations, very good tutorial.
    Can you help me? In a tutorial showing the following example:
    Figure 1.3 text here.
    Figure 1.56 text here.
    The idea is to change in both cases ‘text here’ using positive lookbehind: (?<=Figure\s\d\.[\d+]\s).+
    But applying this style does not work well. Only it applies well in the first case.
    Do you know how to fix it? thank you very much.

    • August 19, 2015    

      hi Julian, have you considered using numbered lists instead of GREP for this? Basically if the ‘1’ in the number is a level 1 number (sourced from a numbered heading or chapter number?), and the .3 a level 2 number, you would set-up a numbered list in InDesign for the figures that has Number option set to Figure ^1.^# and you could apply a Character Style to that text, so it appears differently from the rest of the text.

      Alternatively consider using a GREP style within the paragraph style that formats the full paragraph, with exception of the ‘Figure n.n’, which you change through the GREP style by assigning a Character Style to it:

      (Figure\s\d+\.\d+|Figure\s\d+)\s

  2. Maik Maik
    September 30, 2013    

    A lookbehind in relation to text being read from left to right is confusing, because everyone would understand that in “99c” the “99” is before the c, not behind, as you describe above

  3. max max
    March 18, 2013    

    what about if you have multiple characters in the lookbehind or lookahead? I can’t seem to get it right:

    http://stackoverflow.com/questions/15472989/how-to-fix-this-grep-statement

    • March 20, 2013    

      Max, Sorry, but I’m not quite sure, what you are looking at doing in that question (bit confusing as it’s not all in English). But try looking at PDF I posted with notes on GREP for InDesign, there might be a good example in it for you. You can download it here: http://bit.ly/Z8JoSl (2.4Mb PDF). Maybe that will help? — Cari

  4. March 4, 2013    

    What a great Series

    • March 5, 2013    

      @Jim thanks 🙂 glad you enjoyed these 🙂 … still working on Summary Notes from the InDesign User Group Meeting, they will be done by the end of the week I hope.

Leave a Reply

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