我正在尝试从文本中提取所有包含指定单词的句子。
txt="I like to eat apple. Me too. Let's go buy some apples."
txt = "." + txt
re.findall(r"\."+".+"+"apple"+".+"+"\.", txt)
但它正在返回我:
[".I like to eat apple. Me too. Let's go buy some apples."]
代替:
[".I like to eat apple., "Let's go buy some apples."]
有什么帮助吗?
最佳答案
不需要正则表达式:
>>> txt = "I like to eat apple. Me too. Let's go buy some apples."
>>> [sentence + '.' for sentence in txt.split('.') if 'apple' in sentence]
['I like to eat apple.', " Let's go buy some apples."]
关于Python提取包含单词的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16032832/