python - 正则表达式忽略负向后查找和匹配之间的所有内容

标签 python regex python-3.x

我知道几乎每个正则表达式问题都必须被提出并回答,但我在这里:

我想要一个正则表达式来匹配:

"alcohol abuse"
"etoh abuse"
"alcohol dependence"
"etoh dependence"

但不匹配

"denies alcohol dependence"
"denies smoking and etoh dependence"
"denies [anything at all] and etoh abuse"

负面的lookbehind是显而易见的,但如何不匹配最后两个示例?

到目前为止,我的正则表达式如下所示:

re.compile("(?<!denies\s)(alcohol|etoh)\s*(abuse|dependence)")

我不能在负向后查找中包含贪婪运算符,因为该操作仅适用于要评估的固定长度字符串。

我更愿意一步完成此操作,因为它提供给接受一个正则表达式作为参数的函数。

谢谢指点

最佳答案

如果您无法安装任何模块,您可以重新编写表达式并检查组 1 是否为空:

import re
rx = re.compile("(denies)?.*?(alcohol|etoh)\s*(abuse|dependence)")

sentences = ["alcohol abuse", "etoh abuse", "alcohol dependence", "etoh dependence",
             "denies alcohol dependence", "denies smoking and etoh dependence", "denies [anything at all] and etoh abuse"]

def filterSentences(input):
    m = rx.search(input)
    if m and m.group(1) is None:
        print("Yup: " + sent)

for sent in sentences:
    filterSentences(sent)

这会产生

Yup: alcohol abuse
Yup: etoh abuse
Yup: alcohol dependence
Yup: etoh dependence

如果您有多个否认(即不喜欢...),只需更改第一个标题组即可。

关于python - 正则表达式忽略负向后查找和匹配之间的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409587/

相关文章:

python-3.x - ValueError : Input to `.fit()` should have rank 4. 得到形状为 : (10, 20, 50, 50, 1) 的数组

python - 为什么此打印会更改以下结果?

python - Tkinter 将背景图像调整为窗口大小

python - 有没有一种简单的方法可以删除大多数变量,但保留加载的数据用于正在进行的代码?

regex - 我需要在 sed 命令行中引用什么?

regex - 配置单元查询 regexp_extract

javascript - RegEx JS 从另一个字符串中提取字符串匹配条件

python - 我怎样才能运行与 tkinter 一起不断循环的东西?

python - flask-sqlalchemy - PostgreSQL - 为表定义特定模式?

python - 如何在 sklearn 中使用分层交叉验证处理多类