Let's say I'm trying to match /dog.*lab/
against this text:
"I have a dog. My dog is a black lab. He was created in a laboratory."
Greedily, it would match "dog. My dog is a black lab. He was created in a lab".
I want to find the matches that are narrowest from both sides. If I use the ungreedy modifier like/dog.*?lab/
or /dog.*lab/U
it will match less but still too much:
"dog. My dog is a black lab"
Is there a way to make my search ungreedy from the left also, thus matching only "dog is a black lab"?
Much thanks. Sorry for the contrived example.
You could use a look-ahead assertion that excludes the occurrence of dog
between dog
and lab
:
/dog(?:(?!dog).)*?lab/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With