1

I am struggling to find some GREP to find lines before a string when I need to exclude the string itself from the selection.

In this example, I need to find the 2 lines before "Fox", but exclude "Fox" itself.

Apple   
Banana   
Carrot   
Duck  
Egg  
Fox  
Giraffe

"((.*(\n)){2})Fox" returns "Duck Egg Fox", so I just need to exclude "Fox" from the selection.

1 Answer 1

1

You can use lookaheads in InDesign GREP queries, so you can just search for:

(.*\n){2}(?=Fox)

That will match Duck[break]Egg[break] (with the line breaks).

Some of the parentheses in your regex expression are not necessary, unless you need to capture each line as a subexpression as well.

(If your lines are delimited by paragraph marks, you would of course use \r instead of \n.)

1
  • Thanks for your time Janus - it worked exactly as I hoped
    – RobC
    May 19, 2022 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.