非贪婪的正则表达式并不是最接近的选择

sno*_*ndy 4 regex

我的正则表达式没有选择与内部文本最接近的'cont'对.我该如何解决这个问题?

输入:

cont cont ItextI /cont /cont
Run Code Online (Sandbox Code Playgroud)

正则表达式:

cont.*?I(.*?)I.*?/cont
Run Code Online (Sandbox Code Playgroud)

比赛:

cont cont ItextI /cont
Run Code Online (Sandbox Code Playgroud)

匹配我需要:

cont ItextI /cont
Run Code Online (Sandbox Code Playgroud)

Tim*_*ker 12

cont(?:(?!/?cont).)*I(.*?)I(?:(?!/?cont).)*/cont
Run Code Online (Sandbox Code Playgroud)

只会匹配最里面的块.

说明:

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"
Run Code Online (Sandbox Code Playgroud)

这明确禁止cont/cont出现在开头cont和待拍摄文本之间(以及在该文本和结束之间/cont).