Introducing GREP Styles (1)

089_12_applied

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.

089_01_newcharst

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

089_02_namestyle

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.

089_03_clearoverride

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.

089_04_editbodytext

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.

089_05_newgrepstyle

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.

089_06_applystyle

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.

089_07_openbracket

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.

089_08_anycharacter

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.

089_09_oneormore

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 \(.+?\)

089_11_finished

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.

089_12_applied

Video Tutorial

This Post Has 17 Comments

  1. 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

  2. Cari Jansen says:

    Hi Jochen. Great site! Thanks.

  3. 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

  4. Cari Jansen says:

    @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’.

  5. Erica Gamet says:

    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!

  6. Cari Jansen says:

    @Erica Gamet: you are most welcome! Glad I could help.

  7. CAMILO says:

    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(?=”)

  8. Cari Jansen says:

    @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(?=”)

  9. Jane Tenenbaum says:

    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

  10. Jane Tenenbaum says:

    I may have just figured out the above, just nested style…

  11. Cari Jansen says:

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

  12. Rob Richman says:

    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?

  13. Cari Jansen says:

    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.

  14. Stephen King says:

    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

  15. Leandro says:

    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?

  16. Cari Jansen says:

    hi Leandro, yes that is possible, you use a combination of a Postive Lookbehind and Lookahead, with the parenthesis in Look-codes and the .+ (any character) in between. if you hop onto http://www.indesignusergroup.com and type ‘grip on grep’, select the ‘speaker notes’ I wrote for one of the meetings, it has some good examples in it for you. Hope they help.

  17. Leandro says:

    Those notes are really very helpful, I learned a lot! Thanks!

Leave A Reply