is now part of CreativePro.com!

A Little GREP Gripe

40

Here’s a little GREP quirk I stumbled on a while back.

When you do a GREP Find/Change and include Find Format settings, InDesign only finds instances where the formatting is applied to content matched by the entire GREP expression. And that may leave you feeling rather unsatisfied if you use any of the Lookahead or Lookbehind features in your GREP search.

Consider this example. You have a document with lists of text and numbers set in black and red.

You need to find all instances of red numbers that follow a comma. That seems easy enough. In the Find/Change dialog box, you can create a GREP search with the Positive Lookbehind feature to search for items based on what precedes them.

You tell InDesign to find numbers that are preceded by a comma and a space

and filled with red.

Unfortunately, your search will only turn up instances where the commas are also red. Numbers preceded by commas filled with any other color will not be found, just like those jelly donuts in the coffee shop.

I find this frustrating because the way I see it, the GREP expression matches digits that come after the comma and the space. You can see this when you execute the Find/Change.

See how only the numbers are highlighted? InDesign is just taunting us now. It knows we’re only looking for red numbers, but it won’t give them to us.

You can also use the same expression in a GREP style and the formatting will only be applied to the numbers.

So why doesn’t it work the same in a Find/Change? I have to shrug my shoulders like that kid in the coffee shop and say I don’t know. Maybe one of the GREP gurus out there can explain this behavior. It’s a disappointing limitation for GREP Find/Change, and if you ever run into it you’ll have to find a workaround.

Or just order the bagels instead. ;)

Editor in Chief of CreativePro. Instructor at LinkedIn Learning with courses on InDesign, Illustrator, Photoshop, GIMP, Inkscape, and Affinity Publisher. Co-author of The Photoshop Visual Quickstart Guide with Nigel French.
  • Tournier says:

    Hello,
    I can’t explain this behavoir (Peter Kahrel or Teus de Joung perhaps ?), but you can at first insert tags to mark all red numbers (123) and in a second time match only numbers between tags and delete them (https://twitpic.com/2p6mem)
    Bonne journée

  • Marc Autret says:

    Hi Mike,
    hi Laurent,

    The only explanation I see is that the LookBehind expression is restricted to the Find Format too. Very frustrating indeed.

    @+
    Marc

  • valedot says:

    Ciao,
    Ma Lookbehind non cerca quello che è tra le parentesi (che è nero) e cattura quello che segue?

  • valedot says:

    Hello,
    But Lookbehind not seeking what is in the parentheses (which is black) and capture what follows?

  • Harbs says:

    The formatting restrictions apply to the whole expression — including the lookbehind. I can see the logic in both including the lookbehind and not…

  • Marc Autret says:

    Hi valedot,

    Let me clarify what I said above.

    In Mike’s query, the LookBehind part:

    (?<=, )

    means: “I want to have a comma and a space before what I’m looking for.”

    Then, it sounds that the “Find Format” condition added to the query ?i. e. capturing RED characters? also applies to the LookBehind part.

    So the LookBehind actually means:
    “I want to have a RED comma and a RED space before what I’m looking for.”

    @+
    Marc

  • Mike! Thanks for bring this up!
    Unless I’m missing something, I believe the behavior of InDesign is correct. There is a conjunction “AND” between the GREP expression and Find Format. It means both conditions (GREP & Find Format) need to be met in order to find an occurrence. The Positive Look Behind pattern (comma space) still needs to be RED although it’s excluded from the final result.
    Hope this makes sense.

  • Marc Autret says:

    We would like CS6 to include a new checkbox in the FC GREP dialog:
    “Apply formatting restrictions to FOUND text only”

    ;-)

  • Jongware says:

    I have an evil little imp, somewhere deep inside, so let me propose this one that will work on your sample!

    ((?<!, )|(?<=, ))\b\d+

    I’ll let this sink in for a couple of days before I explain what’s actually very, very wrong with this GREP — that is, unless someone else figures it out :-D

  • Marc Autret says:

    Wow!!! It works!

    Does the pattern:

    (?<!XXXX)\bYYYY

    deactivate the Find Format restrictions on XXXX?

  • Marc Autret says:

    …Ahhhh!

    Ok, I get it ;-)

  • Marc Autret says:

    Just for fun, try Jongware’s killer-tip on:

    text, 123
    text: 123
    text@ 123
    text 123
    text. 123
    etc.

  • Fantastic try Jongware!
    Well spotted Marc!

  • Jan Pillaert says:

    Jongware’s solution will find any string of digits that is formatted in Red. Very cunning.

  • Jongware says:

    Yes, but Marc found the flaw — it no longer checks for a comma before!

    The ‘evil trick’ was in the negative lookbehind:

    (?<!, )\b\d+

    will find any string of numbers with no red text on its left — the comma in there is a red herring, it works just as good with the ‘any’ character:

    (?<!.)\b\d+

  • Guido Benigni says:

    HEY! Can someone tell me what’s goin’ on?
    I DON’T get it :(
    Can please someone explain?

  • Tournier says:

    :-) bien joué Marc. Ah, sacré Jongware, tu nous épateras toujours !

  • Marc Autret says:

    Jongware’s pattern is:

    ((?<!, )|(?<=, ))\b\d+

    which can be schematized into:

    (LookBehind+ OR LookBehind?)\b\d+

    which can be translated into:

    Find or don’t find the comma-space before “\b\d++”, I don’t care!

    which is equivalent to:

    \b\d+

  • Marc Autret says:

    Oups! My schema should have been written:

    (LookBehind? OR LookBehind+)\b\d+

  • F vd Geest says:

    Also see the post here about acronyms!
    I came up with a new one:
    (?<=\W)\u\u+(?=\W)

    See the posts here:
    https://creativepro.com/automating-small-cap-acronyms.php

  • Eugene Tyson says:

    Jongware to the rescue again – he should wear a cape

  • And thus the difference in time zones strikes again… I go to sleep reading the post and wake up to see that every expert in Europe has chimed in! Wonderful! I enjoy a good discussion.

  • Mike Rankin says:

    Eugene, you took the words right out of my mouth. These folks are the Superheroes of GREP. I think Jongware could use GREP to find a needle in a haystack.

    However, I think a mere mortals should be able to tell InDesign to find things like I put the post and have it work they way they want. I really want that checkbox that Marc mentions. Definitely going on my CS6 wishlist.

  • Jongware says:

    Oh I agree (about the checkbox, not the cape).
    It should be allowed to “format” Search text directly in the Find What field — bold, italics, fonts, color, and whatnot.

    It’s nigh on impossible to find an italic “!” followed by a bold “?”.

  • Eugene Tyson says:

    @Mike – absolutely agree.

    @David – I did the same thing, I saw the post and woke up and there loads of comments. It’s like a GREP 24/7 hotline!

    The internet is amazing, post a question on a relevant site and someone somewhere is reading it, knows the answers and posts it back, day or night. Truly remarkable.

  • Eugene Tyson says:

    Now that I think about it – I have a feeling I may be explaining the origin of the term to “post” something on a website!

    I have a feeling that if Jongware couldn’t come up with a GREP he could have scripted it, perhaps? But perhaps the script would be too specific to be worth effort. :)

  • Jeremy says:

    Maybe it’s too early in the morning, or else I’m a bit thick — probably both — but won’t Jongware’s solution simply find any set of red digits? Whereas the object of the exercise was to find only red digits preceded by a comma and a space?

    Sorry in advance — I know this is a really stupid question!

  • Jongware says:

    Don’t worry Jeremy, you are totally correct! That’s the ‘evil’ thing — a lot of smoke & flashes, but in the end, as Marc explains, it’s just cheating.

    It does work on Mike’s sample, but only because every number (in red or otherwise) always has a comma in front.

    I said it was evil, din’t I? ;-)

  • Jeremy says:

    Ah — I see. I guess it was a pretty stupid question, as the irony was lost on me!

  • orielwen says:

    This has been bugging me for months! It’s also true of character styles and other formatting. Glad it’s receiving a bit of publicity ? I’d love for this to be fixed.

  • Dave2theC says:

    I’ve always been interested in knowing GREP better and being able to utilize it. Can anyone recommend a “GREP for Sheer Idiots” guide? Some of the things I’ve found are expression cheat sheets that don’t really explain the nuts and bolts. Thanks in advance.

  • Mike Rankin says:

    Dave2theC-

    Peter Kahrel’s ebook is awesome.
    https://oreilly.com/catalog/9780596156015

    Also, try out the GREP tutor at Rorohiko:

    https://www.rorohiko.com/greptutor/GrepTutor.html#

    which is an interactive tool that lets you see what your GREP expression matches on the fly.

    Also, you can explore GREP right in InDesign if you use the technique I described in this blog post: https://creativepro.com/adventures-in-grepland.php

  • Dave2theC says:

    Thanks much, Mike.

  • Dave2theC – ,
    my favorite was in the beginning Michael Murphy, InDesign Styles:
    https://www.theindesigner.com/blog

  • francis says:

    for me this grep style is working. check it once again

    (?<=)\d+

    thanks.
    Francis

  • Nikki says:

    Could someone please let me know if there is a way to do a grep search and replace for a space following a bold run-in of one or more words and change it to an en space?

  • No matter if some one searches for his necessary thing, so he/she desires
    to be available that in detail, thus that thing is maintained over here.

  • Good blog post. I definitely appreciate this site.
    Stick with it!

  • >