捕获最小的子字符串,而不匹配前面额外的匹配回溯

  • 本文关键字:前面 回溯 不匹配 字符串 regex
  • 更新时间 :
  • 英文 :


我试图在两个已知字符串内匹配一个短语。但是,如果有多个回溯,即使使用延迟量词,也会出现在捕获组中。例如:

在python中使用正则表达式:

(?<=Lookbehind:)(.*?)(?= Lookahead)

将匹配下面的粗体子字符串,当我只想匹配"Text to Capture"

Lookbehind:Test Lookbehind: Text to Capturelook - ahead
Lookbehind:Lookbehind: Text to Capture向后看
向后看:要捕获的文本超前

捕获"要捕获的文本"的更好的查询是什么?通配符组,而匹配尽可能少?

一种方法是在第一个正向后查找之后插入一个负向前查找,它断言Lookbehind是最后一个:

(?<=Lookbehind: )(?!.*Lookbehind: )(.*?)(?= Lookahead)

演示解释:

(?<=Lookbehind: )   assert that what precedes is "Lookbehind: "
(?!.*Lookbehind: )  but also assert that we CAN'T find another "Lookbehind: "
later in the input
(.*?)               text to capture
(?= Lookahead)      assert that what follows is " Lookahead"

最新更新