对于以下代码:
t1 = 'tyler vs ryan'
p1 = re.compile('(.*?) vs (.*?)')
print p1.findall(t1)
输出是:
[('tyler', '')]
但我会预料到这一点:
[('tyler', 'ryan')]
我发现如果我添加一个分隔符,我可以让它工作:
t2 = 'tyler vs ryan!' # Notice the exclamation mark
p2 = re.compile('(.*?) vs (.*?)!') # Notice the exclamation mark
print p2.findall(t2)
输出:
[('tyler', 'ryan')]
有没有一种方法可以在没有自定义分隔符的情况下获得我的匹配项?