Python正则表达式变量负查找

Python正则表达式变量负查找,python,regex,regex-negation,Python,Regex,Regex Negation,我试图找到各种术语的出现,这些术语之前没有另一组术语。通常,如果我在前面的组中有一个术语,我可以使用负回溯,但是Python有一个零宽度的假设,这似乎不是事实。我看到的唯一解决方案是运行两个正则表达式,一个用于显示我要查找的内容,另一个用于确认前面的组项不存在。必须有一种更优雅、更有效的方法来做到这一点。有人能帮忙吗 测试句是: 10 day trip excludes flights 由于单词“flights”前面带有“excludes”,因此确保其不匹配的正则表达式如下: (?:witho

我试图找到各种术语的出现,这些术语之前没有另一组术语。通常,如果我在前面的组中有一个术语,我可以使用负回溯,但是Python有一个零宽度的假设,这似乎不是事实。我看到的唯一解决方案是运行两个正则表达式,一个用于显示我要查找的内容,另一个用于确认前面的组项不存在。必须有一种更优雅、更有效的方法来做到这一点。有人能帮忙吗

测试句是:

10 day trip excludes flights
由于单词“flights”前面带有“excludes”,因此确保其不匹配的正则表达式如下:

(?:without|not including|doesn\'?t include|exclud(?:es|ing))\s*(?:flights?(?:\s+tickets)?|airfare|airline tickets?)
(?:flights?(?:\s+tickets)?|airfare|airline tickets?)
不过,我想确保包含某些文本。我可以用以下几点来证实这一点:

(?:without|not including|doesn\'?t include|exclud(?:es|ing))\s*(?:flights?(?:\s+tickets)?|airfare|airline tickets?)
(?:flights?(?:\s+tickets)?|airfare|airline tickets?)
所以这将匹配“包括机票”和“和机票”,但不匹配“没有机票”

匹配字符串的一些示例包括:

including flights
includes flights
plus flights
flights are included
including airfare
and airfare
not including flights
flights are not included
excluding flights
without airfare
不匹配字符串的一些示例包括:

including flights
includes flights
plus flights
flights are included
including airfare
and airfare
not including flights
flights are not included
excluding flights
without airfare

你可以试试下面的正则表达式

^(?=.*?(?:flights|airfare))(?:(?!without|not includ(?:ing|ed)|doesn\'?t include|exclud(?:es|ing)).)*$

发布一些匹配字符串和非匹配字符串的示例。我添加了一些匹配字符串和非匹配字符串。你是说这个吗?这就是基础。但是,它与前面没有这些特定术语的任何内容相匹配。你可以在这里看到一个例子:这个怎么样?太棒了,我没有想到使用前瞻。