+ 1

Problem with Authentication! Practice in Python Core?

Am I mistaken or is there a flaw in the provided solution of this practice problem? The premise is that a user imputed password should contain at least one uppercase letter as well as one number. The provided solution is: if re.search(r"[A-Z][0-9]*",input()): print("Password created") else: print("Wrong format") Since the (*) metacharacter means 0+ repetitions the password doesn't actually need to have a number in it, it would match any password as long as it has a capital letter. I first thought that the correct solution was to replace the (*) with a (+) as the (+) metacharacter looks for 1+ repetitions. I.E. if re.search(r"[A-Z][0-9]+",input()): However this still doesn't work as it requires the password to have a capital letter that is immediately followed by a number. So A3xample works but Aexampl3 does not. I'm aware that there are ways to do this using look ahead however as this hasn't been covered in the lessons so far my solution was to create an 'if and' statement. password = input() if re.search(r'[A-Z]+',password) and re.search(r'[0-9]+',password): print('Password created') else: print('Wrong format') Am I correct that this provided answer is flawed or am I missing something? Additionally is there a more efficient solution that doesn't involve look ahead? Any input is appreciated.

24th Mar 2022, 6:43 PM
Caleb
3 Answers
+ 2
You could chain patterns with |. If the password is valid, there must be a substring in it that starts with a number, goes on with any character or none and then ends with an uppercase letter. Or the substring starts with an uppercase letter and then goes on with any character or none and then ends with a number. That is, you can search for a pattern like this: '[0-9].*[A-Z]|[A-Z].*[0-9]' (It reads "number, any or none, uppercase – or – uppercase, any or none, number") If such a substring that renders the password valid exists in the password, re.search() will return anything but None. If no such substring exists, it will return None.
24th Mar 2022, 8:55 PM
Lisa
Lisa - avatar
+ 1
# I hope it will be useful password = input() pattern = "^((?=\S*?[^A-Z])(?=\S*?[a-z])(?=\S*?[0-9].{1,})(?=\S*?[!@#$%&*]{1,}).{6,})\S
quot; result = re.fullmatch(pattern, password)
25th Mar 2022, 12:09 AM
CodeStory
CodeStory - avatar
0
nums="0123456789" chars="!@#%&*
quot; cc=0 nc=0 word=input() for i in word: if i in chars: cc+=1 elif i in nums: nc+=1 if nc >= 2 and cc >=2 and len(word) >= 7: print("Strong") else: print("Weak") i think it looks same but rules are length more 7 and contains number and special character
24th Mar 2022, 8:00 PM
Ahmed Yahya
Ahmed Yahya - avatar