How to apply lazy quantifier in this scenario?

I am trying to combine an occurrence with a regex:

to (. *?) CITY [\ d]

against

John from beautiful CITY1 in sdfsf to dsfs in sf to abc CITY2 to CITY3 for 3 days

I get two matches:

to dsfs in sf to abc CITY2 
to CITY3

My problem is that I need a regular exp, for which I could get a match as "for abc CITY2" instead of "to dsfs in sf for abc CITY2".

I read about lazy quantifiers, but couldn't apply it in this scenario.

+4
source share
1 answer
\bto\b(?:(?!\bto\b).)*?CITY\d+

Try it. Check out the demo.

http://regex101.com/r/iZ9sO5/7

lookahead, , to to. to City.

(?!\bto\b) negaitve lookahead

.)*

+3

All Articles