#小练习 正则表达式匹配邮箱名称

s='[email protected],    [email protected],[email protected], [email protected], [Master][email protected],[科比][email protected],[email protected],[email protected]'


要求:输出[email protected] 或 [xxx][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 re

s='[email protected],    [email protected],[email protected], [email protected],  [Master][email protected],[科比][email protected][email protected],[email protected]'

p=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 实现

你可能感兴趣的:(#小练习 正则表达式匹配邮箱名称)