问题
def check_password(pw):
global reason
while True:
if not re.search(string.ascii_lowercase,pw):
flag = False
reason = get_error(4)
break
elif not re.search(string.ascii_uppercase, pw):
flag = False
reason = get_error(1)
break
elif not re.search(string.digits, pw):
flag = False
reason = get_error(2)
break
elif not re.search(string.punctuation, pw):
flag = False
reason = get_error(3)
break
else:
flag = True
print("Valid Password")
break
return flag
flag = False
min_length = 5
#password = input("Please enter your password:")
password = getpass.getpass("Enter Password:")
if len(password) >= min_length:
check_password(password)
print(password)
if flag == True:
print("Valid Password!")
else:
print(reason)
else:
print("Your password is not long enough.")
Here I am using regex to validate a password. I was successful if I hardcode the pattern "[A-Z]", "[0-9]", but now if I use string library constants, I am getting the message from the 1st check, irrespective of anything else wrong in the password.
Can you tell me where I am wrong, syntactically or semantically?
回答1:
The constants only contain chunks of characters, but you want to create character classes out of them. That means you need to enclose all of them with [ and ]. Also, you need to make sure some characters are escaped (^, -, ] and \), otherwise the punctuation regex will not be matching what you expect.
So, you need to replace string.ascii_lowercase with f"[{string.ascii_lowercase}]", string.ascii_uppercase with f"[{string.ascii_uppercase}]", string.digits with f"[{string.digits}]", string.punctuation with f"[{string.punctuation.replace("\\", "\\\\").replace("-", r"\-").replace("^", r"\^").replace("]", r"\]")}]".
If your Python environment is older than 3.7 and does not support string interpolation, use str.format, e.g. if not re.search(r"[{}]".format(string.ascii_lowercase),pw) and so on.
来源:https://stackoverflow.com/questions/59140483/python-password-validation-unable-to-use-constants-from-string-library-in-regex