python贪婪和非贪婪
Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符;
非贪婪则相反,总是尝试匹配尽可能少的字符。
在"*","?","+","{m,n}"后面加上?,使贪婪变成非贪婪。
>>> s="This is a number 234-235-22-423" >>> r=re.match(".+(d+-d+-d+-d+)",s) >>> r.group(1) '4-235-22-423' >>> r=re.match(".+?(d+-d+-d+-d+)",s) >>> r.group(1) '234-235-22-423' >>> |
>>> re.match(r"aa(d+)","aa2343ddd").group(1) '2343' >>> re.match(r"aa(d+?)","aa2343ddd").group(1) '2' >>> re.match(r"aa(d+)ddd","aa2343ddd").group(1) '2343' >>> re.match(r"aa(d+?)ddd","aa2343ddd").group(1) '2343' >>> |