【发布时间】:2026-01-31 00:40:02
【问题描述】:
我有一个类似于电子邮件正文的文本,如下所示。
To: Abc Cohen <abc.cohen@email.com> Cc: <braggis.mathew@nomail.com>,<samanth.castillo@email.com> Hi
Abc, I happened to see your report. I have not seen any abnormalities and thus I don't think we
should proceed to Braggis. I am open to your thought as well. Regards, Abc On Tue 23 Jul 2017 07:22
下午 托尼·斯塔克写道:
那我有一个关键词列表如下。
no_wds = ["No","don't","Can't","Not"]
yes_wds = ["Proceed","Approve","May go ahead"]
目标:
我想首先搜索上面给出的文本字符串,如果上面列出的任何关键字存在(或存在),那么我想提取这些关键字之间的字符串。在这种情况下,我们有从 no_wds 匹配的 Not 和 don't 关键字。此外,我们还从 yes_wds 列表中匹配了 Proceed 关键字。因此,我要提取的文本列表如下
txt = ['seen any abnormalities and thus I don't think we should','think we should']
我的做法:
我试过了
re.findall(r'{}(.*){}'.format(re.escape('|'.join(no_wds)),re.escape('|'.join(yes_wds))),text,re.I)
或者
text_f = []
for i in no_wds:
for j in yes_wds:
t = re.findall(r'{}(.*){}'.format(re.escape(i),re.escape(j)),text, re.I)
text_f.append(t)
没有得到任何合适的结果。然后我尝试了str.find()的方法,也没有成功。
我试图从here 获得线索。
有人可以帮忙解决这个问题吗?我很想看到任何非正则表达式的解决方案,因为正则表达式有时并不合适。话虽如此,如果有人可以提出基于正则表达式的解决方案,我可以在其中迭代列表,这是受欢迎的。
【问题讨论】:
-
要明确一点,“from word”来自
no_wds,“to word”来自yes_wds,与no_wds中的“from word”在同一索引中? -
是的,你是对的。
标签: python