给定文本,我想找出发生在未知之前的单词
text="the women marathon unknown introduced at the summer olympics los angeles usa and unknown won"
items=re.finditer('unknown',text). #as there are 2 unknown
for i in items:
print(i.start()) #to get index of 2 unknown
输出为
19
81
现在如何分别提取出现在两个未知数之前的单词?
对于第一个未知,我应该得到,女人。
第二个未知的是我应该去美国
不带
re
,带itertools.groupby
(doc)的版本:印刷品:
此表达式可能接近此处所需的表达式:
测试关于芬德尔
测试重新查找
表达式在this demo的右上角面板上解释,如果您希望探索/简化/修改它,在this link中,如果您愿意,您可以一步一步地观察它如何与一些示例输入匹配。你知道吗
短进近:
输出:
相关问题 更多 >
编程相关推荐