Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Grep for negative pattern match?

Return to Member Forum

  • Author
    Posts
    • #56423

      Is there any way I can get a grep search to look for everything except a certain string?

      For example, the search string “a .+ night” would match “a cold, quiet night”, “a warm, muggy night”, “a cool, dark night” and “a freezing cold night”. Is there any way I can tell it to ignore the phrases containing the word “cold”, and match all the others?

    • #56425
      Anonymous
      Inactive

      as(?!cold).+snight

      something like that

    • #56430

      Eugene Tyson said:

      as(?!cold).+snight


      That still matches 'a freezing cold night' (it only eliminates matches where 'cold' is the first word after 'a'). The word 'cold' might appear anywhere ('a cold and gloomy night').

      If I try changing it to

      as(.+)?(?!cold)(.+)?snight

      then all the phrases containing 'cold' start getting matched as well, as .+ matches anything including 'cold'.

      It might not be possible for GREP to do what I want, because it processes strings linearly, and to do this properly I think it would have to get to the end of the phrase and see whether it ended in 'night' before skipping back and seeing if it had encountered a 'cold' anywhere along its length.

    • #56431

      Sure it's possible! It's a matter of grouping:

      as((?!cold).)+?snight

      works, because the outer parentheses group is what gets repeated, and inside the parentheses every single character is checked to not have 'cold' in front of it.

      For added fun, you can add word boundaries before and after 'cold', so it will not reject 'a scolding hot night' ;-)

      as((?!bcoldb).)+?snight

      (Ed.: Added '?' after the pluses so it will match shortest possible strings only — usually, that's what you'd want.)

    • #56438

      Ah, fantastic! That's exactly what I was looking for! Thank you!

Viewing 4 reply threads
  • The forum ‘General InDesign Topics (CLOSED)’ is closed to new topics and replies.
Forum Ads