During a recent ‘Demystifying GREP’ presentation I gave for the InDesign User Group in Perth, I found myself almost physically jumping into a sentence when explaining the concept of Positive Lookahead. I’ve made my speaker notes available: What is GREP? (2.4Mb PDF download).
I promised the group I’d write up some posts on lookaheads and behinds specifically catering for designers 🙂 This is the first in a series of posts.
What’s GREP?
GREP allows you to locate text in a paragraph based on a pattern in which it appears, something that distinguishes that particular bit of text from surrounding text.
For instance text that:
- appears between parentheses
- is preceded by a number
- follows a full stop
GREP in InDesign allows you to:
- Change the physical appearance of specific text by automatically applying a character style to it by using a GREP style . GREP styles can be accessed from Control panel menu, Paragraph panel menu or as part of Paragraph Style Options.
- Change the text itself, it’s order, inserting other text before or after it and more, through Find/Change (GREP).
The Positive Lookahead
To understand what a positive 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 at what’s in front of you…
In the example below you are looking at finding the word ‘chapter’ when it is followed by a space and a number.
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 positive lookahead has a positive 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).
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 Positive Lookahead.
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.
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.
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 Positive Lookahead at this stage. The second post in this series covers the Negative Lookahead.
@Camilo GREP Find/Change has many advantages over Text Find/Change. As you say, you could try and do something like this with Text Find/Change – but, it would take multiple steps. With GREP you could locate the word ‘chapter’ in one step, and replace it with another word, or change the formatting of it. I’ve purposely tried to use a simple example to explain the concept of the Positive Lookahead to beginners 🙂 Once you start combining the many GREP options in a single Find/Change things become really exciting indeed 🙂
It seems similar to use a f/change search?:
Chapter ^9 or Chapter ^9^9
Change to a colour and later change these numbers in colour to black.
Of course using grep is just one step.
I would like have seen a hardest example.
But is the first time I see a perfect drawing and explanation.