Thanks for coming to InDesignSecrets.com, the world's #1 resource for all things InDesign!

Formatting Important Phrases With GREP

In the past, I have worked with clients to build catalogs where the phrase “USA made” requires special formatting. Typically, the client wants “USA” to be red, and “made” to be blue. But only when “USA” precedes “made,” and “made” follows “USA.” This seems complicated but is a pretty understandable GREP exercise when it is broken down into pieces.  So I thought it would be worth a blog post.

Granted, you probably don’t need to format “USA made” in red and blue, but the need to format two words differently only when they appear together is a common design pattern, so hopefully you can learn from this example and adapt it for your own use.

We’re going to do this with a GREP paragraph style. You can learn more about GREP paragraph styles here and here.

1. Create some sample text that includes the phrase “USA made” in a few separate locations.

2. Create a new Paragraph style called “body text” and apply this to the text.

3. Create a new Character style called “red” that is formatted with the color red.

4. Create a new Character style called “blue” that is formatted with the color blue.

5. Edit the “body text” paragraph style, and fill in the GREP Style section exactly as shown below.

Let’s break the 2 GREP expressions down into simple pieces:

Expression 1:

\<USA\>\s+(?=\<made\>)

USA will match the word “USA”. GREP expressions are case-sensitive by default, so it will only match “USA” in all caps.

Adding \< and \> around “USA” tells InDesign to match only a whole word. Probably not really necessary in this case, since I can’t think of any English words that begin or end in all-caps “USA”. But if we were dealing with different words, this would be important.

\s is shorthand for a “whitespace” character. This matches any type of space. Regular spaces, non-breaking spaces, em spaces, en spaces, etc.

The + means “one or more” so we are looking for “USA” followed by one or more spaces followed by “made”.

However, we don’t want to apply the “red” character style to the entire phrase “USA made”, only to “USA”. So we need to wrap the \<made\> string in parentheses with ?= at the front. (?=) is called a “positive lookahead”. In this case, it tells InDesign that when it finds an occurrence of “USA”, it should “look ahead” and see if it is followed by “made”. If it is followed by “made”, then consider it a match, but don’t include “made” in the match.

Expression 2:

(?<=\<USA\>)\s+\<made\>

This expression is much like expression 1, but the opposite.

\s is shorthand for a “whitespace” character. This matches any type of space. Regular spaces, non-breaking spaces, em spaces, en spaces, etc.

The + means “one or more”, so we are looking for one or more spaces.

made will match the word “made”, but only when all lowercase.

Adding \< and \> around “made” tells InDesign to match only a whole word.

So we are looking one or more spaces followed by the whole word “made”.

However, we don’t want to apply the “blue” character style to the entire phrase “USA made”, only to “made”. So we need to wrap the \<USA\> string in parentheses with ?<= at the front. (?<=) is called a “positive lookbehind.” In this case, it tells InDesign that when it finds an occurrence of “made”, it should “look behind” and see if it is preceded by “USA”. If it is preceded by “USA”, then consider it a match, but don’t include “USA” in the match.

Hopefully you’ve learned a thing or two from this exercise, and can use one or more of these techniques in one of your own projects!

Keith Gilbert

Keith Gilbert

Keith Gilbert is a digital publishing consultant and educator, Adobe Certified Instructor, Adobe Community Professional, conference speaker, lynda.com author, and contributing writer for various publications. His work has taken him throughout North America, Africa, Europe, and Asia. During his 30 years as a consultant, his clients have included Adobe, Apple, Target, the United Nations, Best Buy, General Mills, Lands' End, and Medtronic. Follow him on Twitter @gilbertconsult and at blog.gilbertconsulting.com.
Keith Gilbert

Latest posts by Keith Gilbert (see all)

  • - November 30, -0001
Tags
Related Articles
Comments

6 Comments on “Formatting Important Phrases With GREP

  1. Is there anything GREP can’t do? Well, maybe. While it can work wonders like the above, it still seems unable to distinguish web addresses from other text. I’ve got a number of GREP scripts that be far more useful if GREP S&R inside ID allowed me to specify whether or not web addresses are to be included in the search.

    A typical case involves replacing hyphens with n-dashes or m-dashes, In the scientific books I work with, perhaps half the hyphens will be part of web addresses where that hyphen is never to be changed. I haven’t found a way to tell ID to skip that text. Is there anyway to do a search that’d exclude all Endnote Text paragraph styled text? That’d eliminate most of them.

    And yes, I realized that detecting an entire web address, particularly its end, is not easy. A few years back I tried to find a GREP script that does that flawlessly and drew a blank. And I realize that ultimately the flaw lies with GREP itself and not its ID implementation.

    Still, it would be nice if GREP intrinsically knew if text was part of a web address or not. In that, GREP seems to still live the world of UNIX pre-web. It illustrates a product so good, few realize that it’d benefit from a few enhancements.

  2. GREAT lesson in GREP features via this one “USA made” example. Thank you!

    Which Dr. Seuss book has a Thneed?

  3. Is it possible to format an entire paragraph if certain text is present? I have some paragraphs that include the letters “QS” and wondered if the paragraph could change color wherever QS occurs.

Leave a Reply

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