gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:32 27 4
gpt4 key购买 nike

我有这样一个字符串,

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").'

现在,我想使用关键字 championunderdog 提取当前的 championunderdog

这里真正具有挑战性的是两个竞争者的名字都出现在括号内的关键字之前。我想使用正则表达式并提取信息。

下面是我做的,

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'}

关于python - 从字符串中提取出现在关键字之前的单词/句子 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48953985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com