GREP: Different behaviour of positive lookahead/lookbehind

Learn / Forums / General InDesign Topics / GREP: Different behaviour of positive lookahead/lookbehind

Viewing 7 reply threads
  • Author
    Posts
    • #77081
      Chris Court
      Member

      This seems to be a simple grep search, but I can’t figure it out and it’s driving me crazy!

      I’m trying to apply a style to a particular word, but only when it is part of a quote. Let’s say the word is “blah”.

      “blah” may be, but is not necessarily the first or last word in a given quote, so I’m attempting to look for the following pattern: Quote mark>zero or more characters>blah>zero or more characters>quote mark

      I would expect that the following would give me the result I’m after: (?<=(“.*))blah(?=(.*”)) , but this does not work.

      The second half of the grep – blah(?=(.*”)) – seems to work as expected, and identifies my word when followed by zero or more characters and then a quote mark.
      However, the first half of the grep – (?<=(“.*))blah – which should look for the quote mark and zero or more characters BEFORE the word, fails.

      I’ve tried multiple variations on the syntax, with no success. I have found that it appears to be the * – “repeat zero or more times”, that appears to break the positive lookbehind.

      Any input would be much appreciated!

      Best
      Chris

    • #77083
      David Blatner
      Keymaster

      Yes, the positive look-behind cannot function with an ambiguous number of characters. It has to be a specific string. There are other ways of doing this though. In InDesign Magazine issue 73, Peter Kahrel explained the \K grep command, which Adobe added in CS6. So this will find the word blah that shows up after a quote mark (and with or without additional text between the quote and the world blah):
      ".*\Kblah

    • #77084
      Chris Court
      Member

      OK, Interesting… and useful. It works!!

      Unfortunately I don’t have access to the InDesign magazine… so here is my next question: Is there an inverse to \K? An equivalent to a NEGATIVE lookbehind? For example, if I want to locate all examples of “blah” that are NOT preceded by a quote mark and possibly additional text?

      Thanks for your help, David.

      Best
      Chris

    • #77085
      David Blatner
      Keymaster

      Not that I know of.

    • #77086
      David Blatner
      Keymaster

      Well, just to be clear, you could use ^[^"]*blah which means “from the beginning of the paragraph up to the word blah, but only if there is no quote mark.” But that doesn’t find just the word “blah” — it finds the whole enchilada.

    • #77088
      Chris Court
      Member

      OK, thanks again, David.

      Just to simplify my original question, I’m looking for a way to “Find all examples of the word ‘blah’ where there is no quote mark in the paragraph.” Or more specifically, “Find all examples of the word ‘blah’ where there is no quote mark preceding it in the paragraph.”

      If anyone else has any bright ideas as to how to achieve this, I’d love to hear from you!

      Best
      Chris

    • #77093

      I’m not behind a modern ID right now so I cannot check – but would it not work the same with the “fake” positive lookbehind, using \K? Try

      ^[^"]*\Kblah

    • #77097
      Chris Court
      Member

      Wow, that did it. You guys are complete rockstars!!

      I found that non-inDesign versions of GREP have a -v “invert switch” command, which I think may have gotten me there. However – and please correct me if I’m wrong – it doesn’t seem to be part of inDesign’s GREP spec.

      Thanks again for your help, David and Jongware. This place is such a great resource.

      Best
      Chris

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
>