"""5.1 验证电子邮件字符串是否合法要求:1. @之前不能包含(中杠)-,及其他$&等符号,但可以包含., 开头字母和数字2. 统一命名is_valid_email()3. @之后数字或者字母 后缀.com|.gov|.net任一结尾参考:测试其合法性1. someone@gmail.com 真2. bill.gates@microsoft.com 真3. 597116500@qq.com 真4. r-bob@example.com 假5. 597116500@qq.net 真"""import mathimport redef email_path_before_chack(str): if not re.compile("^[a-zA-Z0-9][a-zA-Z0-9,'.']*$").match(str): print("邮箱地址输入错误") return False return Truedef email_path_last_chack(str): if not re.compile("^[a-zA-Z0-9,'.']*$").match(str.split(".")[0]): print("邮箱地址后缀名输入错误") return False if not str.split(".")[1] in ["com","gov","net"]: print("邮箱网站地址输入错误") return False return Truedef is_valid_email(email_path): if "@" not in email_path: print("该地址格式不正确,没有@") return if not email_path_before_chack(email_path.split("@")[0]): return if not email_path_last_chack(email_path.split("@")[1]): return print("{}是合法邮箱地址".format(email_path)) returndef main(): while True: email_path = input("请输入邮箱地址:\n") if email_path == "退出": break is_valid_email(email_path)if __name__ == "__main__" : main()程序执行结果为
来源:https://www.cnblogs.com/lyy17759893807/p/12046307.html