[Py003]正则(?<=) (?=)

同样的正则,有时候在re.findall()和re.search()返回值不相同
使用(?<=) (?=)限定一下便没有问题了

string = 'a132dffgb2345'
pattern1 = re.compile('[0-9]([a-z].+?)[0-9]')
re.findall(pattern1, string)
Out[21]: 
['dffgb']
re.search(pattern1, string).group()
Out[22]: 
'2dffgb2'
pattern2 = re.compile('(?<=[0-9])([a-z].+?)(?=[0-9])')
re.findall(pattern2, string)
Out[24]: 
['dffgb']
re.search(pattern2, string).group()
Out[25]: 
'dffgb'

你可能感兴趣的:([Py003]正则(?<=) (?=))