【发布时间】:2020-02-18 17:59:01
【问题描述】:
目标:从电子邮件线程中提取第一封电子邮件
描述:基于对电子邮件的人工检查,我意识到电子邮件线程中的下一封电子邮件总是以一组 From、Sent、To 和 Subject 开头
测试输入:
Hello World from: the other side of the first email
from: this
sent: at
to: that
subject: what
second email
from: this
sent: at
to: that
subject: what
third email
from: this
date: at
to: that
subject: what
fourth email
预期输出:
Hello World from: the other side of the first email
尝试失败:
当第一封电子邮件中有 from: 时,休息后
(.*)((from:[\s\S]+?)(sent:[\s\S]+?)(to:[\s\S]+?)(subject:[\s\S]+))
From、Sent、To 和 Subject 组重复时,跟随失败
([\s\S]+)((from:(?:(?!from:)[\s\S])+?sent:(?:(?!sent:)[\s\S])+?to:(?:(?!to:)[\s\S])+?subject:(?:(?!subject:)[\s\S])+))
第二次尝试works with PCRE(PHP) 时选择了一个不贪婪的选项(标志)。但是,此选项在 python 中不可用,我无法找到使其工作的方法。
【问题讨论】:
-
很抱歉,我在这里忽略了重点。
-
您是否只是想从显示的输入中提取
Hello World from: the other side of the first email? -
是的,没错。假设它是整个邮件链中的第一封电子邮件,需要根据建议的查找发件人、发送人、收件人和主题组的方法进行提取。
-
这对你有用吗? regex101.com/r/dw05sx/6
-
您可以使用捕获组
^(.*)\r?\n\s*\r?\nfrom:.*\r?\nsent:.*\r?\nto:.*\r?\nsubject:.*见regex101.com/r/fLbKb4/1
标签: python regex string-matching