Introducing GREP Styles (1)
InDesign CS3 introduced the ability to perform GREP Find/Changes, making it easier for us to apply character styles to text pattern strings such as ‘all text’ between parentheses. InDesign CS4 continued the “GREP-theme’ through its ability to lock this pattern based application of character styles into your Paragraph Styles.
The following tutorial is part of a 4-part series on GREP Styles.
Part 2: Change the appearance of numbers (figures) within a given paragraph style
Part 3: Converting text typed in Capital Letters to Small Caps (OpenType feature)
Part 4: Formatting price tags containing dollars and cents with multiple character styles
In this first tutorial we start with a simple GREP style tutorial that looks at how we can automatically apply a character style to all text between parentheses.
Task at hand
All of the paragraphs for which the text between parenthesis must be set in Italic have already had a Paragraph Style “Body Text” applied to it.
Define a Character Style
In preparation for the GREP style, I prefer to create the Character Styles required prior to defining the GREP style components (Character Style and Expression).
To define a Character Style: Highlight some text in the Body Text paragraphs and change its formatting. I’ve changed the Font Style to Medium Italic.

Next choose New Character Style from the Character Styles panel menu and Name the Style. I’m naming mine “Medium Italic”.

Click OK to add the style to the Control Panel. I’m purposely not clicking “Apply Style to Selection’, as the text I formatted with Medium Italic is only used as the basis for the Character Style definition.

Because we set some of the text in Medium Italic (Font Style), our Paragraph Style now indicates with the ‘+’ icon that this format is not native to the Paragraph Style. Let’s remove this format override, by clicing the Clear Overrides icon at the bottom of the Paragraph Styles panel.
It is now time to add the GREP Style to our Paragraph Style.

GREP Style
Right-click the Paragraph Style and select Edit “Body Text” to edit the style. The Paragraph Style Options dialog appears. Click GREP Styles.

First of all lets set the Character Style that we’re going to apply to the GREP Expression we’re inserting in just a moment. From the Apply Style drop down choose the Character Style created earlier.

The Character Style we want to apply is “Medium Italic”.
Oops did you forget to create the Character Style? Don’t worry, from InDesign CS4, you have the ability to create your Character Style right here in the Paragraph Style Options dialog. Have a look at the bottom of the Apply Style pop-up and notice the “New Character Style”.
GREP Expression
It’s time to get GREPPING… so what was that pattern again we were looking for? An Open Parenthesis character, followed by any type of text (alpha-numerical, spaces etc. all allowed), finished off with a Closing Parenthesis character.
Symbols
If you are already a GREP wiz you might be able to enter the required GREP expression in the To Text field. However, I personally prefer constructing the expression bit-by-bit. The Special Characters For Search pop-up is a great help in constructing the expression. InDesign will automatically insert the correct expression data.

Our Open Parenthesis Character is part of the Symbols group.
As you select this component, you might notice that the text entered in the To Text field is now set to \(. Certain symbols are reserved by GREP for particular expression components. The Parenthesis form part of this group of characters. For our GREP Expression to recognise the “(” as a literal character it must be preceded by a “\” character. This “\” is referred to as an Escape Character.
Each of the following characters is reserved by GREP:
* ^ } ] ) $ . { [ ( | + ? ~If you want to use these characters as a literal character in a GREP expression, you must escape them with the “\”.
Wildcards
Next we’re after our next part of the expression: The any text or words etc. that are inserted prior to the closing parenthesis.

When you are looking for ANYTHING… Think of the fact that you know that this ANYTHING might be an “a” or a “b” or really any character of the alphabet, and even numbers or spaces.
Wildcards are a way of defining an “ANYTHING”. And as we’re looking for any type of character, not just Digits (numbers), Letters (alphabetical) or White Space (spaces), we’re choosing Wildcards > Any Character.

A “.” is added to the GREP expression.
Repeat
You might think at this stage that you’re there… Not quite… GREP Expressions need to know home many times on ef these Any Characters can occur… So we need to know tell the Expression how many times this Any Character can repeat itself.
Let’s assume we don’t encounter “()” (without text between the parentheses). So we’re looking at One or More occurrences.
Shortest Match
So what would happen if there is more than one set of “()” within a paragraph? Well, this is where the Shortest Match option comes in handy. Shortest Match looks for the first occurrence of the “(” and the first occurrence of the “)”, the shortest possible string it can find in the paragraph, and that is exactly what we’re after
So let’s choose Repeat > One or More Times (Shortest Match). This adds “+?” to the expression.
We’re almost there at this stage. All we need to add to our expression now is the Closing Parenthesis character. We jump back to Symbols for that one.
Choose Symbols > Close Parenthesis Character
Our finished expression is \(.+?\)

Any text between brackets is now set using our GREP style. Click OK to apply the GREP style to the Paragraph Style and see the finished result.

Video Tutorial
Topics: Adobe, GREP, InDesign | 15 Comments »
July 5th, 2009 at 11:11 pm
Hi Carl,
very good, thanks. Think it is very important to communicate these new technologies proudly and intensively.
Tried something similar just two days ago: with the simple example of figures typogracilly organized in groups of three. Don’t mind the German; GREPs are international
http://blog.smi.ch/2009/07/03/automatische-3er-gruppe-zahlen/
Jochen
July 5th, 2009 at 11:26 pm
Hi Jochen. Great site! Thanks.
July 5th, 2009 at 11:28 pm
BTW: Michael Murphy has shown in his great new book on InDesign Styles that Adobe’s list of GREP Metacharacters is incomplete. What about a quick change of
http://help.adobe.com/en_US/InDesign/6.0/WSa285fff53dea4f8617383751001ea8cb3f-6f59a.html
July 6th, 2009 at 12:07 am
@Jochen:
Yep
http://carijansen.com/2009/05/03/styles-styles-and-more-styles/
I reckon it’s a must have book for anyone who is serious about learning to work with InDesign productively and using ‘best practice approach’.
March 17th, 2010 at 7:08 am
Thank you, Cari, for being the only resource that mentions shortest match. I’ve been trying to figure out how to find HTML-like tags in a doc, strip them out, and format them when there was more than one instance in a paragraph. Shortest match…who knew one little question mark could mean so much? Thank you, thank you!
March 26th, 2010 at 9:29 am
@Erica Gamet: you are most welcome! Glad I could help.
September 29th, 2010 at 3:40 pm
What if i want to replace the “any character” character, for a hole word. For example, I want to replace the period “.” for the letter “a” and thats how im doing that, but it just doesnt work.
(?<=“)\a(?=”)
October 1st, 2010 at 8:41 am
@Camilo
What is it you are trying to do, find the letter ‘a’ when it is between quotes-marks?
you would just use the literal character ‘a’ in that case. E.g. (?<=”)a(?=”)
September 30th, 2011 at 6:36 am
Hi! Ever since I switched from Quark, I have been trying to do what Quark does easily: When I make a paragraph, e.g., “Tx,” I want to create a character style (e.g., “Tx-char”) for that paragraph to use all the time (not to have just have “none” as its char. style). Is it possible to use GREP to make the whole paragraph use the Tx-char style? btw: I’m using a trial version of Indesign CS 5.5
September 30th, 2011 at 6:40 am
I may have just figured out the above, just nested style…
October 6th, 2011 at 8:08 am
@Jane
Please note there is a significant difference between how InDesign and Quark handle Character Styles.
Character styles when created are best designed by first applying the paragraph style over the top of which they will be applied… then define the new style. This means only those attributes that differ from the rest of the text will be included in the style.
December 31st, 2011 at 12:50 am
Hello! I have a client whose brand name must be in all-caps the FIRST time it appears, then upper/lower thereafter. I’ve set-up an all-caps character style and – in my paragraph style – have set-up the following GREP (using ‘Brand’ instead of the real name here)…
Brand+?
…I THOUGHT adding the Repeat > One or More Times (Shortest Match) would do the trick, but no luck…it’s capping ALL mentions of the name within the paragraph…Any advice?
December 31st, 2011 at 11:51 pm
Hi Rob, hmmmm… don’t think you can do that with a ‘GREP’ style. I’d be inclined to use a script for that as scripts can count ‘occurrences’. GREP styles don’t count occurrences… but merely look for a pattern. Shortest Match basically looks for the shortest occurrence of a pattern. For instance if a paragraph contains two words in brackets… and you are looking for a word in brackets, it will end the ‘pattern’ at the first closing bracket rather than the second. And it would then find the second word in brackets as well. Hope this helps.
January 7th, 2012 at 9:19 pm
Cari Jensen-great tut on one thing Grep Styles can do. I have worked a bit in the Help Files about Grep but for some reason this tutorial seems to be more understandable to me. Thanks so much and I am going on to the other 3 at this time.
Happy New Year!
Steve
February 4th, 2012 at 2:27 am
Thanks for the tutorial, Cari!
Is it possible to set up a GREP style that uses some characters as a guide to a pattern, but that does not affect those characters?
For instance, in the example of the tutorial, would it be possible to apply the character style to the text inside the parenthesis, but not to the parenthesis characters?