python搜索分隔的单词



我正试图从一个python列表中提取分离的多个单词,其中两个不同的列表作为查询字符串。我的句子列表是

lst = ['we have the terrible HIV epidemic that takes down the life expectancy of the African ','and I take the regions down here','The poorest are down']
lst_verb = ['take','go','wake']
lst_prep = ['down','up','in']
import re
output=[]
item = 'down'
p = re.compile(r'(?:w+s+){1,20}'+item)
for i in lst:
output.append(p.findall(i))
for item in output:
print(item)

有了这个,我可以从列表中提取单词,但我只想提取分离的多个单词,即它应该从列表中抽取单词"我把区域放在这里"。此外,我想使用lst_verb和lst_rep中的单词作为查询字符串。例如

re.findall(r lst_verb+'*.b'+ lst_prep)

谢谢你的回答。

您可以使用类似的正则表达式

(?is)^(?=.*b(take)b)(?=.*?b(go)b)(?=.*b(where)b)(?=.*b(wake)b).*

匹配多个单词像这个例子

使用函数从谓词和prep创建regex字符串。希望这能帮助

最新更新