利用Python和正则表达式验证hotmail邮箱的格式

利用Python和正则表达式验证hotmail邮箱的格式

因为想学Python的缘故再加上老师让练习正则表达式,于是就有了这篇博客。再加上hotmail邮箱的格式还是很丰富的,这样的话也多了些乐趣。

首先分享一下我自己验证得到的格式要求:

  1. 开头必须是字母。反例:1eee@hotmail.com
  2. 结尾不能是.反例:eee.@hotmail.com
  3. 不能1个以上的.连用 反例:eee..eee@hotmail.com
  4. 除去@hoymail.com,最大长度是64个字符,最短是1个字符
  5. 字符串中只能包含[a-zA-Z0-9-_.]。 反例:eee+eee@hotmail.com
  6. 必须以"@hoymail.com"结尾。 反例fefefefefefe

其他的比如两个-连用和两个_连用都是可以的,比如e--__@hotmail.com,亲测通过。

自己也可以想一些测试点试一下:点我跳转

代码

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 用途:hotmail邮箱地址格式验证
# 作者:软件-1802-张伟涛
# 验证规则:
#     1.开头必须是字母 反例:1eee@hotmail.com
#     2.结尾不能是"."  反例:eee.@hotmail.com
#     3.不能1个以上的"."连用 反例:eee..eee@hotmail.com
#     4.除去"@hoymail.com",最大长度是64个字符,最短是1个字符
#     5.字符串中只能包含"[a-zA-Z0-9-_.]"。 反例:eee+eee@hotmail.com
#     6.必须以"@hoymail.com"结尾。 反例fefefefefefe
# 说明:
#     1.Bug标识验证不通过的原因或者验证通过
#     2.Debug为0为正常使用模式;为1时为debug模式,当处于debug模式时程序会输出相应步骤的信息

import re

Debug = 0
Bug = 0

def debug():
    global Bug
    if not Debug:
        return
    if Bug == 1:
        print('字符串过长或过短')
    elif Bug == 2:
        print('没有以"@hotmail.com"结尾或者只有"@hotmail.com"')
    elif Bug == 3:
        print('开头不是字母')
    elif Bug == 4:
        print('有多个"."连用')
    elif Bug == 5:
        print('以"."结尾')
    elif Bug == 6:
        print('有非法字符')
    elif Bug == 0:
        print('满足全部条件')

# 正则表达式版本
def cheMailAddress_RE(str):
    global Bug
    Bug = 0
    s = re.sub(r"@hotmail.com$", "", str)# 除去后缀
    if len(str) > 76 or len(str) < 12:
        Bug = 1                     # 加上"@hotmail"之后最长是76(64 + 12)个字符,最短是13(1 + 12)个字符
    elif re.match(r".+@hotmail.com$", str) == None:
        Bug = 2                     # 必须以"@hotmail.com"结尾
    elif re.match(r"[a-zA-Z]", s) == None:
        Bug = 3                     # 开头必须是字母
    elif re.search(r"\.\.", s) != None:
        Bug = 4                     #不能有1个以上的"."连用
    elif re.match(r".+\.$", s) != None:
        Bug = 5                     #不能以"."结尾
    elif re.search(r"[^a-zA-Z0-9-_.]", s) != None:
        Bug = 6                     #只能包含"[a-zA-Z0-9-_.]"
    #满足以上即为正确格式
    debug()                         #输出调试信息
    if Bug == 0:
        return 1
    else:
        return 0

for i in range(10):
    if cheMailAddress_RE(input('请输入一个邮箱地址:')):
        print('格式正确!')
    else:
        print('格式有问题……')

代码(不用正则表达式版本)

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# 用途:hotmail邮箱地址格式验证
# 作者:软件-1802-张伟涛
# 验证规则:
#     1.开头必须是字母 反例:1eee@hotmail.com
#     2.结尾不能是"."  反例:eee.@hotmail.com
#     3.不能1个以上的"."连用 反例:eee..eee@hotmail.com
#     4.除去"@hoymail.com",最大长度是64个字符,最短是1个字符
#     5.字符串中只能包含"[a-zA-Z0-9-_.]"。 反例:eee+eee@hotmail.com
#     6.必须以"@hoymail.com"结尾。 反例fefefefefefe
# 说明:
#     1.Bug标识验证不通过的原因或者验证通过
#     2.Debug为0为正常使用模式;为1时为debug模式,当处于debug模式时程序会输出相应步骤的信息

import re

Debug = 0
Bug = 0

def debug():
    global Bug
    if not Debug:
        return
    if Bug == 1:
        print('字符串过长或过短')
    elif Bug == 2:
        print('没有以"@hotmail.com"结尾或者只有"@hotmail.com"')
    elif Bug == 3:
        print('开头不是字母')
    elif Bug == 4:
        print('有多个"."连用')
    elif Bug == 5:
        print('以"."结尾')
    elif Bug == 6:
        print('有非法字符')
    elif Bug == 0:
        print('满足全部条件')

# 非正则表达式版本
def cheMailAddress(str):
    global Bug
    Bug = 0
    if len(str) > 76 or len(str) < 12:
        Bug = 1                     # 加上"@hotmail"之后最长是76(64 + 12)个字符,最短是13(1 + 12)个字符
    elif str[-12:] != r"@hotmail.com" or len(str) == 12:
        Bug = 2                     # 必须以"@hotmail.com"结尾
    elif not str[0].isalpha():
        Bug = 3                     # 开头必须是字母
    elif str.find('..') != -1:
        Bug = 4                     #不能有1个以上的"."连用
    elif ord(str[-13]) == ord('.'):
        Bug = 5                     #不能以"."结尾
    else:
        for c in str[:-12]:
            if not (c.isalnum() or c == '-' or c == '_' or c == '.'):
                Bug = 6             #只能包含"[a-zA-Z0-9-_.]"
    #满足以上即为正确格式
    debug()                        #输出调试信息
    if Bug == 0:
        return 1
    else:
        return 0

for i in range(10):
    if cheMailAddress(input('请输入一个邮箱地址:')):
        print('格式正确!')
    else:
        print('格式有问题……')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值