+ 1
why does not this code pass 10 and 11 test cases of password validation?
I'm trying to solve password validation in code coach. it says it must at least contains two digits and two special characters. and the total length of string must at least be 7. I thought using regex should be a good option to solve this problem. but it does not pass 10 and 11 test cases. import re password = input() pattern = r"^(?=(.*\d){2})(?=.*[a-zA-Z])(?=(.*[!@#$%]){2,})[0-9a-zA-Z!@#&$%*]{7,}" if re.match(pattern, password): print("Strong") else: print("Weak")
4 odpowiedzi
+ 1
thanks Jayakrishna🇮🇳
I have changed my pattern to :
^(?=(.*\d){2})(?=(.*[!@#$%]){2,})[0-9a-zA-Z!@#&$%*]{7,}
and it will consider your sample as "Strong".
but even with this change, it doesn't pass 10 and 11 test cases.
+ 1
Total length should be atleast 7 so in which it must have at least 2 digits and 2 special characters. Means it must pass this types sample :ex:
@#12345
+ 1
Jayakrishna🇮🇳
oops, there was another error in my regex. I forgot to add '&' in one of my lookahead statements.
after changes my pattern looks like this:
pattern = r"^(?=(.*\d){2,})(?=(.*[!@#&$%*]){2,})[0-9a-zA-Z!@#&$%*]{7,}"
thanks!
0
&* misses. yes. now It passes all.
You're welcome..