4

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!

1 Answer 1

1

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.

Live demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this RegEx is causing find() to be valid twice when n=1. For n=2, it's fine (only one find).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.