Reputation:
I'm writing a function that sometimes will receive a massive calls (near 2k/minute) and it should check if the password is valid.
My requirements are simple:
At least 5 characters;
At least 1 number;
At least 1 uppercase letter;
At least 1 lowercase letter;
At least 1 special character.
For now, I have code my function as:
import re
def check(p):
return re.match(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{5}$", p)
I have some questions:
Why my regex only works for 5 characters? If I try to validate a string with 6 characters, it fails.
Isn't regex kinda slow during peak time? What are other alternatives?
Upvotes: 1
Views: 85
Reputation: 39800
The following should do the trick (note the {5,}
):
re.match(r"(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{5,}$", p)
In other words, {5,}
means 5 or more as opposed to {5}
that means exactly 5.
Upvotes: 1