【发布时间】:2018-02-23 18:16:39
【问题描述】:
我有一个这样的字符串,
my_str ='·in this match, dated may 1, 2013 (the "the match") is between brooklyn centenniel, resident of detroit, michigan ("champion") and kamil kubaru, the challenger from alexandria, virginia ("underdog").'
现在,我想使用关键字 champion 和 underdog 提取当前的 champion 和 underdog 。
这里真正具有挑战性的是两个竞争者的名字都出现在括号内的关键字之前。我想使用正则表达式并提取信息。
以下是我所做的,
champion = re.findall(r'("champion"[^.]*.)', my_str)
print(champion)
>> ['"champion") and kamil kubaru, the challenger from alexandria, virginia ("underdog").']
underdog = re.findall(r'("underdog"[^.]*.)', my_str)
print(underdog)
>>['"underdog").']
但是,我需要结果,champion as:
brooklyn centenniel, resident of detroit, michigan
和underdog 为:
kamil kubaru, the challenger from alexandria, virginia
如何使用正则表达式来做到这一点? (我一直在搜索,是否可以从关键字中返回几个或单词以获得我想要的结果,但还没有运气)任何帮助或建议将不胜感激。
【问题讨论】:
标签: python regex keyword matching