非贪婪的正则表达式不选择最接近的选择 [英] Non-greedy regex does not pick the closest choice
本文介绍了非贪婪的正则表达式不选择最接近的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的正则表达式没有选择最接近内部文本的cont"对.我该如何解决?
My regex does not pick the closest 'cont' pair to the inner text. How can I fix that?
输入:
cont cont ItextI /cont /cont
正则表达式:
cont.*?I(.*?)I.*?/cont
匹配:
cont cont ItextI /cont
我需要的匹配:
cont ItextI /cont
推荐答案
cont(?:(?!/?cont).)*I(.*?)I(?:(?!/?cont).)*/cont
只会匹配最里面的块.
说明:
cont # match "cont"
(?: # Match...
(?!/?cont) # (as long as we're not at the start of "cont" or "/cont")
. # any character.
)* # Repeat any number of times.
I # Match "I"
(.*?) # Match as few characters as possible, capturing them.
I # Match "I"
(?: # Same as above
(?!/?cont)
.
)*
/cont # Match "/cont"
这明确禁止 cont
或 /cont
出现在开头的 cont
和要捕获的文本之间(以及该文本之间和结束 /cont
).
This explicitly forbids cont
or /cont
to appear between the opening cont
and the to-be-captured text (and between that text and the closing /cont
).
这篇关于非贪婪的正则表达式不选择最接近的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文