Java RegEx to retrieve the nth group

I need to extract only the first match from the following:

Input (please ignore any possibility of treating it as XML using XPath, etc. - this is just an example):

<city>NYC - New York </city><city>PAR - Paris</city><city>NYC - New York </city><city>MIA - Miami</city>

RegEx:

(?si).*?(?:NYC\s-\s)([^<]*)

As you can see, I already made it lazy, however both New Yorks are captured. If I leave him greedy, only the last will be captured. I need to limit using a regular expression (not via a method find) to capture only the first (in fact, it would be best to control which one I want, for example the 8th case). I am afraid this will make the regex very dirty. Any help? Thank!

+4
source share
1 answer

:

(?si)(?:(?:NYC\s-\s)([^<]*).*?){n}

n - , .

+1

All Articles