re正则匹配中括号贪婪与非贪婪

贪婪

str = [abc[def]gh]
pat = r"(\[.*\])"
result = re.findall(pat, str,re.M)
pringt(result)

>>> [abc[def]gh]

非贪婪

str = [abc[def]gh]
pat = r"(\[.*?\])"
result = re.findall(pat, str,re.M)
pringt(result)

>>> [abc[def]

 

posted @ 2020-10-14 17:17  欧仔  阅读(287)  评论(0编辑  收藏  举报