Python正则表达式匹配开头结尾

字符 功能
^ 匹配字符串开头
$ 匹配字符串结尾

问题代码

# -*- coding: utf-8 -*-
# @Time    : 2019/10/4 14:47
# @Author  : 币行者
# @Email   : [email protected]
# @File    : 匹配邮箱地址.py

import re

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

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

优化后代码

在163.com末尾加上$

# -*- coding: utf-8 -*-
# @Time    : 2019/10/4 14:52
# @Author  : 币行者
# @Email   : [email protected]
# @File    : 正确匹配邮箱地址.py

import re

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

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

你可能感兴趣的:(Python正则表达式匹配开头结尾)