代码:
#coding=utf-8
import re
s = 'abc111def abc222def abc345xyz abc678xyz'
reg = r'(?<=abc)((?!abc).)+(?=xyz)'
imgre = re.compile(reg)
re.search(imgre, s)
print ('re.search = ' + re.search(imgre, s).group())
print ('re.findall =', re.findall(imgre, s))
结果:
re.search = 345
re.findall = ['5', '8']
跪求解释findall为啥不是['345', '678']
正则表达式改为:
r'(?<=abc)((?:(?!abc).)+)(?=xyz)'
原因是用括号捕获重复的数字,只捕获了一个,依旧是最后一个数字。