import re def is_valid_email(addr): pattern = re.compile(r"^[\w]+(\.[\W]+)*@+[\w]+(\.[\w])+") result = pattern.match(addr) if result: return True else: return False if __name__ == "__main__": while True: addr = input("请输入邮箱号码:\n") if addr == "q": break print(is_valid_email(addr))
打印信息:
请输入邮箱号码: 956897565@qq.com True 请输入邮箱号码: 956897565@qq.net True 请输入邮箱号码: 956897-565@qq.com False