美文网首页有梦才会去努力
Python正则表达式匹配开头结尾

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

作者: 黑猫编程 | 来源:发表于2019-10-04 14:56 被阅读0次
字符 功能
^ 匹配字符串开头
$ 匹配字符串结尾

问题代码

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

import re

email_list = ["BXingZhe@163.com", "ShiJiTech@163.comheihei", "xypip@qq.com"]

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   : xypip@qq.com
# @File    : 正确匹配邮箱地址.py

import re

email_list = ["BXingZhe@163.com", "ShiJiTech@163.comheihei", "xypip@qq.com"]

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正则表达式匹配开头结尾

    本文链接:https://www.haomeiwen.com/subject/slcspctx.html