在前面学习了前向搜索的否定模式,其实也存在后向搜索的否定模式,就是说先把字符串匹配过后,再来回过头去判断是否不需要的字符串,它的语法是这样: (?<!pattern)。由于这个语法是搜索前面已经匹配的字符串,所以必须是固定长度的字符串,并且不能是组索引,这与前向搜索有区别的。例子如下:
结果输出如下:
Candidate: first.last@example.com
Match: first.last@example.com
Candidate: noreply@example.com
No match
深入浅出Numpy
五子棋游戏开发
http://edu.youkuaiyun.com/course/detail/5487
#python 3.6
#蔡军生
#http://blog.youkuaiyun.com/caimouse/article/details/51749579
#
import re
address = re.compile(
'''
^
# An address: username@domain.tld
[\w\d.+-]+ # username
# Ignore noreply addresses
(?<!noreply)
@
([\w\d.]+\.)+ # domain name prefix
(com|org|edu) # limit the allowed top-level domains
$
''',
re.VERBOSE)
candidates = [
u'first.last@example.com',
u'noreply@example.com',
]
for candidate in candidates:
print('Candidate:', candidate)
match = address.search(candidate)
if match:
print(' Match:', candidate[match.start():match.end()])
else:
print(' No match')
结果输出如下:
Candidate: first.last@example.com
Match: first.last@example.com
Candidate: noreply@example.com
No match
在这里同样实现识别不作回复的EMAIL地址。