Home > Blockchain >  Regex: match closest starting and closing strings
Regex: match closest starting and closing strings

Time:08-16

Assume the following word sequence

BLA text text text  text text text BLA text text text text LOOK BLA text my text text LOOK text text text LOOK BLA text text LOOK

What I would like to do is find all occurrences of "my text" and all the strings up to the BLA immediately before "my text" and the LOOK immediately after "my text"

The output I expect is:

BLA text my text text LOOK

I tried this but what it does is extract all patterns starting with BLA and ending in LOOK not only the pattern containing "my text"

BLA(?:(?!BLA|LOOK)([\s\S]))*?LOOK

CodePudding user response:

BLA(?:(?!BLA).)*?my text.*?LOOK

https://regex101.com/r/C7yxVo/1

If I`m getting your intentions right

  • Related