Wednesday, December 02, 2009

An InDesign GREP tutorial

Steve (who attended my "Power through your work with InDesign Styles" seminar in September) wrote:

"I am trying to find an InDesign GREP style that would find select every character (paragraphs included) between any amount of tabs and the next instance of a bullet. Any Ideas?"

The GREP string that I came up with to do what Steve asks is this:

(?<=\t)[^~8\t]+(?=~8)

I'll take this apart and try to help you understand it piece by piece.

(?<=\t)

The \t means to look for a tab character. The (?<=) is called a "positive lookbehind," which tells InDesign to not include the thing it finds in the search result. So (?<=\t) means to "look back" for a tab character, but don't include the tab character in the search result.

[^~8\t]+

The square brackets create what is called a "character class," which means to "match any of the things in these brackets". In this case, in the brackets we have a ~8 which is a bullet, and a \t which is a tab character. So [~8\t] would mean to match any character that is a bullet or tab. But if you look closely, we have a ^ before the bullet in the character class. This means "not". So [^~8\t] means to match any character that is NOT a bullet or tab.

Finally, you'll notice a + after the character class. This means to match the character class "one or more times". So [^~8\t]+ means to match "one or more characters that are not bullet or tab characters".

(?=~8)

As we saw above, the ~8 is a bullet character. The (?=) is a "positive lookahead," which, like a "positive lookbehind" tells InDesign to not include the thing it finds in the search result. So (?=~8) means to "look ahead" for a bullet character, but don't include the bullet character in the search results.

Putting it all together, we have (?<=\t)[^~8\t]+(?=~8) which means to:

Look for one or more characters that are not bullets or tabs, that are preceded by a tab character and followed by a bullet, (but don't include the tab or bullet in the search results).

The (?<=\t)[^~8\t]+(?=~8) string can be used in InDesign in two different places:

1. The GREP tab of the Find/Change dialog box, to do a search based on the GREP expression, or

2. The GREP Style tab of the Paragraph Style Options dialog box, which allows you to have InDesign assign a character style automatically any time that the GREP expression is matched.

6 comments:

  1. Hello,

    Good tips. The end of the regex can, perhaps, be completed with ? (?=~8?) to find a paragrah beginning by a bullet followed by a tabulation, but not necessary followed by another paragraph with a bullet, ie the last paragraph.
    Laurent Tournier
    www.indigrep.com

    ReplyDelete
  2. Thanks for the comment, Laurent. Yes, my GREP string is "strict", meaning that it will only find a tab, followed by one or more non-tab and non-bullet characters, followed by a bullet. So if there is a tab near the end of the story without a following bullet, it will not match from the tab to the end of the story...it "needs" the bullet.

    However, adding the ? (which means "zero or more times" at the end, as you suggest, means that it will wrongly match a tab followed by one or more non-tab and non-bullet characters followed by a tab.

    ReplyDelete
  3. Like you, my example with ? was "strict". It was OK only for the last paragraph of a story. That is why, I thought about others "cas".
    1) The last paragraph followed by a paragraph without bullet
    2) One paragraph with bullet and tab inside.
    (have a look here http://laurent.tournier.free.fr/bullet_tab.jpg)
    To select both, I found this regex :
    ((?<=~8)|(?<=\t))([^~8\t\r])+

    Best
    Laurent

    ReplyDelete
  4. @gmoeller54: Tab length is a "formatting" function, and you can definitely include this in the find/change. To do this, create a Paragraph Style containing the Tab Stop attributes that you want (.25", etc.). Then, in the Find/Change dialog box, just leave ^t in the "Change to:" field, then click on the "More Options" button. Click on the magnifying glass icon to the right of the "Change Format" field, and choose the Paragraph Style you created in the "Style Options" category.

    In other words, you can search for a character (5 spaces in your case), and replace it with another character (a tab character) along with some formatting specified by a Paragraph Style.

    ReplyDelete
  5. @one2love

    Use the following:
    (?<=\t).+$

    To break it down :

    (?<=\t) is a Positive Lookbehind for a tab character as in the original post

    .+ represents one or more characters of any type

    $ represents the end of the paragraph.

    ReplyDelete
  6. GREP to me is like doing a Tax Return
    AHHHHHHHHHHHHH!!!!!!
    This page shed just enough light so that I could clean up a spreedsheet that I do every two weeks

    THANK YOU!!!

    GREP still drives me nuts.
    Even more so when everywhere I look people are saying GREP makes working in Indesign easier :)

    ReplyDelete