中的文字:正则表达式搜索词两者是相同
PAGE 1
apple
PAGE 2
apple
banana
PAGE 3
orange
PAGE 4
banana
PAGE 5
pear
PAGE 6
apple
orange
banana
pea
我希望有一个正则表达式,会告诉我每一个页面一个香蕉上,这是第2页和4
事情我已经尝试:
PAGE.*?banana.*?PAGE
但返回PAGE 1和4
PAGE(?!.*?PAGE).*?banana
这是一个尝试向前看,并确保页面和香蕉字之间没有额外的页面,但这没有返回。
(?<=PAGE).*(?=banana)
借来自Regex, get entire string between two keywords。这是返回PAGE 1,匹配最后一个香蕉和第一页之间的所有内容。
我认为环顾四周就是答案,但我无法围绕如何匹配PAGE#和香蕉,但只有PAGE#香蕉。我怎么做?
您需要编号或整个块? – andlrc
你只需要一个温和的贪婪标记解决方案:['PAGE \ d + \ n((?:(?!\ bbanana \ b | \ nPAGE \ d + \ n)。)* \ bbanana \ b(?:(?!\ (* = \ nPAGE \ d + \ n | $)'](https://regex101.com/r/gE1oN3/1)。 –
我只需要这个号码。 – Mike