Start and end of the regular match

character Features
^ Matches the beginning of string
$ End of the string

Example 1: $

Requirements: 163.com match-mail address

#coding=utf-8

import re

email_list = ["[email protected]", "[email protected]", "[email protected]"] for email in email_list: ret = re.match("[\w]{4,20}@163\.com", email) if ret: print("%s 是符合规定的邮件地址,匹配后的结果是:%s" % (email, ret.group())) else: print("%s 不符合要求" % email) 

operation result:

xiaoWang@163.com 是符合规定的邮件地址,匹配后的结果是:xiaoWang@163.com
xiaoWang@163.comheihei 是符合规定的邮件地址,匹配后的结果是:xiaoWang@163.com
[email protected] 不符合要求

Perfect after

email_list = ["[email protected]", "[email protected]", "[email protected]"]

for email in email_list: ret = re.match("[\w]{4,20}@163\.com$", email) if ret: print("%s 是符合规定的邮件地址,匹配后的结果是:%s" % (email, ret.group())) else: print("%s 不符合要求" % email) 

operation result:

xiaoWang@163.com 是符合规定的邮件地址,匹配后的结果是:xiaoWang@163.com
xiaoWang@163.comheihei 不符合要求
[email protected] 不符合要求

Guess you like

Origin www.cnblogs.com/georgexu/p/11204744.html