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.
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).
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.
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.
The To Text area now displays:
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.
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.
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:
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
what about if you have multiple characters in the lookbehind or lookahead? I can’t seem to get it right:
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
What a great Series
@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.