简体   繁体   English

Java RegEx提取第n组

[英]Java RegEx to extract nth group

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): 输入(请忽略任何将其视为XML,使用XPath等的可能性 - 这只是一个例子):

<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). 我需要通过正则表达式(不是通过find方法)来限制只捕获第一个(实际上,最好的是控制我想要的那个,比如第8次出现)。 I'm afraid it will make the regular expression very messy. 我担心它会使正则表达式变得非常混乱。 Any help? 有帮助吗? Thanks! 谢谢!

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. 其中n是您要捕获的事件的序数。

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM