PyCon 2011
本作品采用知识共享署名-非商业性使用-相同方式共享 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: username@domain.tldaddress = 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 = ['first.last@example.com','user@example.org','ignored@example.co.uk','noreply@example.com',]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'
运行结果
first.last@example.com MATCH ('first.last@example.com',)user@example.org MATCH ('user@example.org',)ignored@example.co.uk NO MATCHnoreply@example.com NO MATCH
页:
[1]