s='[email protected], [email protected],[email protected], [email protected], [Master][email protected],[科比][email protected],[email protected],[email protected]'
import re
p=r'\[?\w+\]?\w+@\w+\.\w{2,3},?'
print re.findall(p,s)
# 结果:['[email protected],', '[email protected],', '[email protected],', '[email protected],', '[Master][email protected],', '[email protected],', '[email protected],', '[email protected]']
\w 匹配 任意数字和字母,输出[email protected],而不是[科比][email protected];无法匹配T-Mac中的T-,使用p=r'\[?\w+\]?-?\w+@\w+\.\w{2,3},?' 可以匹配T-Mac
=============================================================================
import rep=r'\[?\S+\]?\S+@\S+\.\w+'
print re.findall(p,s)
# 结果:['[email protected]', '[email protected],[email protected]', '[email protected]', '[Master][email protected],[\xbf\xc6\xb1\xc8][email protected]', '[email protected],[email protected]']
\S 匹配任意非空白字符,s中只有[email protected]、[email protected]、[Master][email protected]、[email protected]前面出现空白,则直到遇到下个空白字符为止,此次匹配结束。所以出现类似 '[email protected],[email protected]' 的列表项。
所以此处建议使用\w 实现