Reputation: 892
I need to extract only the first match of the following:
Input (please ignore any possibility to treat it as XML, using XPath, etc. - it's 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 York are being captured. If I leave it greedy, only the last one is being captured.
I need to limit, via regular expression (not via find
method), to capture only the first one (in fact, the best would be to control which one I want, like the 8th occurrence).
I'm afraid it will make the regular expression very messy.
Any help?
Thanks!
Upvotes: 4
Views: 390
Reputation: 76646
You can use the following regex:
(?si)(?:(?:NYC\s-\s)([^<]*).*?){n}
Where n
is the ordinal number of the occurrence you would like to capture.
Upvotes: 1