Python提取包含单词的句子 [英] Python extract sentence containing word

查看:336
本文介绍了Python提取包含单词的句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文本中提取所有包含指定单词的句子.

I am trying to extract all the sentence containing a specified word from a text.

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."]

请帮忙吗?

推荐答案

In [3]: re.findall(r"([^.]*?apple[^.]*\.)",txt)                                                                                                                             
Out[4]: ['I like to eat apple.', " Let's go buy some apples."]

这篇关于Python提取包含单词的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆