【发布时间】:2018-09-28 08:21:48
【问题描述】:
我想找到一个选定的单词,并从它之前的第一个句点 (.) 到它之后的第一个句点 (.) 取所有内容。
示例:
在文件中调用'text.php'
'The price of blueberries has gone way up. In the year 2038 blueberries have
almost tripled in price from what they were ten years ago. Economists have
said that berries may going up 300% what they are worth today.'
代码示例:(我知道如果我使用这样的代码,我可以在单词 ['that'] 之前找到 +5 并在单词之后找到 +5,但我想找到前后句点之间的所有内容一句话。)
import re
text = 'The price of blueberries has gone way up, that might cause trouble for farmers.
In the year 2038 blueberries have almost tripled in price from what they were ten years
ago. Economists have said that berries may going up 300% what they are worth today.'
find =
re.search(r"(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,5}that(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,5}", text)
done = find.group()
print(done)
返回:
'blueberries has gone way up, that might cause trouble for farmers'
我希望它返回每个带有 ['that'] 的句子。
示例返回(我想要得到的):
'The price of blueberries has gone way up, that might cause trouble for farmers',
'Economists have said that berries may going up 300% what they are worth today'
【问题讨论】:
-
这个
capital = (if get('test') then get('friendly'))应该做什么?这种语法在 Python 中是不可接受的吗?描述你的脚本的逻辑
标签: python python-2.7 python-requests full-text-search