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.

text sample, with chapter preceding a number underlined in green when chapter not preceded by number 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  (‘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).

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 after the word ‘chapter’.
  • From the Special Characters for Search menu, choose Match and select Positive Lookahead.

GREP style dialog

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.

GREP style dialog

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.

GREP style dialog

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.

GREP style dialog, with finished GREP and text example