26.4 re.findall()的非贪婪模式 https://scz.617.cn/unix/201109081058.txt Q: >>> re.findall(r'\[My\](.+)\[/My\]', r'[My]a[/My]\n[My]b[/My]') ['a', 'b'] >>> re.findall(r'\[My\](.+)\[/My\]', r'[My]a[/My]c[My]b[/My]') ['a[/My]c[My]b'] 原始意图是无论re.findall()第二形参是什么,都得到['a', 'b'],但现在第二条命 令没有进行最小匹配,请问有办法解决吗?其中c可以是任意字符、字符串。 A: dxh@nsfocus 对于+、*来说,默认是贪婪模式(greedy),在其后增加一个?号,则切换至非贪婪模 式,或者说进行最小匹配。 >>> re.findall(r'\[My\](.+?)\[/My\]', r'[My]a[/My]c[My]b[/My]') ['a', 'b']