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

Positive Lookahead – GREP for Designers

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

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 [...]

2 Comments

  1. March 23, 2013    

    @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 🙂

  2. Camilo Camilo
    March 10, 2013    

    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.

Leave a Reply

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