python - 有关正则表达式的问题
本文介绍了python - 有关正则表达式的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
In [60]: re.findall('(?m)\s+(?!noreply|postmaster)(\w+)',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[60]: ['sales', 'eng']
In [61]: re.findall('(?m)\s+',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[61]: ['\n ', '\n ', '\n', '\n', '\n']
In [62]: re.findall('(?m)\s+(\w+)',
"""
sales@phptr.com
postmaster@phptr.com
eng@phptr.com
noreply@phptr.com
"""
)
Out[62]: ['sales', 'postmaster', 'eng', 'noreply']
为什么第一、三次的空格和换行没有匹配,而第二次的匹配了?
解决方案
findall优先返回分组的内容 (\w+)
没有分组,就返回整个表达式匹配的内容~ \s+
这篇关于python - 有关正则表达式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文