PyCon 2011 - Hidden Treasures of the Python Standard Library - 邮件地址正则表达式匹配

 

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。


作者:liuyuan_jq

2011-03-30

 

 

 

#!/usr/bin/env python # encoding: utf-8 """Positve look-ahead and negative look-behind assertions """ import re # An email address: [email protected] address = re.compile( r''' # Limit the top-level domains (?=.*/.(?:com|org|edu)$) ^( [/w/d.+-]+ # Account name (?<!noreply) # Ignore "noreply" @[/w/d.+-]+ # Domain name )$ ''', re.UNICODE | re.VERBOSE) candidates = [ '[email protected]', '[email protected]', '[email protected]', '[email protected]', ] for candidate in candidates: # print '{0:25}'.format(candidate), print '%25s' % (candidate,), match = address.search(candidate) if match: print 'MATCH ', match.groups() else: print 'NO MATCH'

 

运行结果

[email protected] MATCH ('[email protected]',) [email protected] MATCH ('[email protected]',) [email protected] NO MATCH [email protected] NO MATCH 

 

你可能感兴趣的:(python,正则表达式,user,domain,library,encoding)