我有以下字符串:
狂野2比1战胜火焰.
我需要从那个字符串中提取球队的名字和分数。在Python中,我做了以下工作:
foo = re.findall(r'The (\w+) won (\d+) - (\d+) over the (\w+)\.', mystring)
现在的问题是,其中有带有空格的团队名称,如下所示:
红翼队以4-3击败了蓝夹克队.
我将如何编写与这两个字符串匹配的regexp?
发布于 2010-12-13 20:45:00
您只需编辑原始regex,在团队名称组中包含空格:
foo = re.findall(r'The ([\w ]+) won (\d+) - (\d+) over the ([\w ]+)\.', mystring)
发布于 2010-12-13 20:44:51
使用([\w ]+)
而不是(\w+)
。
发布于 2010-12-13 20:50:04
如果格式确实如此一致,那么您可以放松一下表达式,它会很好地工作:
foo = re.findall(r'The (.+) won (.+) - (.+) over the (.+).', mystring)
https://stackoverflow.com/questions/4433307
复制相似问题