Python提取包含单词的句子

标签 python regex text-segmentation

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

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/

相关文章:

opencv - 在这种情况下如何分割连接的字符?

python - 如何在 Python3 中找到按数字顺序排列的列表中的数字?

python - 减少不适用于 collections.defaultdict?

mysql - 在 MYSQL 和 PHP 中存储和查询数组或词组

Javascript 用空字符串替换特殊字符

正则表达式:非捕获组仍然显示

java - 使用 NLP 进行句子检测

javascript - onClick 函数导致 “Uncaught SyntaxError: Unexpected token }” 错误

python - 为什么 Mean() 在空 DataFrame 上有不同的行为?

python - 更新数据存储中的 div 页面坐标