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