我试图找到不在另一组术语之前的各种术语的出现。通常情况下,如果我在前面的组中有一个单项,我可以使用负面的lookbehind,但在Python有一个零宽度假设,这似乎不是这种情况。我看到的唯一解决方案是运行两个正则表达式,一个用于存在我正在查找的内容,另一个用于确认前一个组项的不存在。必须有一种更优雅和有效的方式来做到这一点。有人可以帮忙吗?
测试句是:
10 day trip excludes flights
由于“航班”一词前面带有“排除”而确保不匹配的正则表达式如下:
(?: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
答案 0 :(得分:1)
你可以试试下面的正则表达式,
^(?=.*?(?:flights|airfare))(?:(?!without|not includ(?:ing|ed)|doesn\'?t include|exclud(?:es|ing)).)*$