密码输入格式检查
- 要求密码长度为 6 到 20 位
- 密码只包含英文字母和数字
import re pw = input("please input your password:") pwObj = re.compile(r'[\da-zA-Z]{6,20}') t = pwObj.fullmatch(pw) #用fullmatch()方法查看是否整个字符串都匹配 if t == None: print("\nCheck Out:Incorrect password format.\n") print(pwObj.fullmatch(pw)) else: print("\nCheck Out:Correct format.\n") print(pwObj.fullmatch(pw))
运行:(格式正确下)
please input your password:python123 Check Out:Correct format. <re.Match object; span=(0, 9), match='python123'>
运行:(格式不正确下)
please input your password:123
Check Out:Incorrect password format.
None