我想从文本中提取包含指定单词的所有句子。包含单词的Python提取语句
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."]
任何帮助吗?
+1不错的答案!如果你创建一个'txt = txt * 10000',那么'%timeit'结果会更清晰 – Kent
谢谢Kent。我为更大的字符串添加了'%timeit'基准。 – unutbu