0

I have done extensive reading, but have trouble solving this scenario. I want to matching everything in between abc and xyz, in a non greedy way - without any extra abc or xyz in the matched string.

For example, if my input is abc abc xyz xyz, i want to get abc xyz as the output. If I use /abc.*?xyz/g as the expression, it returns abc abc xyz. xyz is matched non greedily. How can I get abc also matched non-greedily?

For the input like, abc pqr abc lmn xyz lmn xyz, output should be abc lmn xyz

Raj
  • 135
  • 2
  • 11
  • 1
    See [my answer](http://stackoverflow.com/a/37343088/3832970). – Wiktor Stribiżew Dec 29 '16 at 17:14
  • Based on @WiktorStribiżew's hint, maybe `abc(?:(?!abc|xyz).)*?xyz`. As Wiktor notes, the tempered greedy token construct is expensive. – Ouroborus Dec 29 '16 at 17:19
  • That is not a hint, that is the answer, see [`abc(?:(?!abc|xyz).)*xyz`](https://regex101.com/r/xU4gM0/6). You do not need to use a lazy quantifier here, since the `xyz` is an alternative in the tempering lookahead. – Wiktor Stribiżew Dec 29 '16 at 17:22
  • Thanks for the link. Following your original post, I foundthat this patterns returns what I needed. `abc(?:(?!abc).)*?xyz` – Raj Dec 29 '16 at 17:23
  • @WiktorStribiżew In that case, `abc(?:(?!abc).)*?xyz` would seem to perform a more efficient search. – Ouroborus Dec 29 '16 at 17:32

0 Answers0