Skip to main content
 首页 » 编程设计

python之从字符串中提取出现在关键字之前的单词/句子之Python

2025年01月19日23三少

我有这样一个字符串,

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 

和失败者是:

kamil kubaru,来自弗吉尼亚州亚历山大市的挑战者

我如何使用正则表达式来做到这一点? (我一直在搜索,如果我可以从关键字中返回几个或单词以获得我想要的结果,但还没有运气)任何帮助或建议将不胜感激。

请您参考如下方法:

您可以使用命名的捕获组来捕获所需的结果:

between\s+(?P<champion>.*?)\s+\("champion"\)\s+and\s+(?P<underdog>.*?)\s+\("underdog"\) 
  • between\s+(?P<champion>.*?)\s+\("champion"\)匹配来自 between 的 block 至 ("champion")并将所需的部分作为命名的捕获组 champion 放在中间

  • 之后,\s+and\s+(?P<underdog>.*?)\s+\("underdog"\)匹配到 ("underdog") 的 block 并再次从此处获取所需的部分作为命名的捕获组 underdog

示例:

In [26]: 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").' 
 
In [27]: out = re.search(r'between\s+(?P<champion>.*?)\s+\("champion"\)\s+and\s+(?P<underdog>.*?)\s+\("underdog"\)', my_str) 
 
In [28]: out.groupdict() 
Out[28]:  
{'champion': 'brooklyn centenniel, resident of detroit, michigan', 
 'underdog': 'kamil kubaru, the challenger from alexandria, virginia'}