+ 2

Python Regular Expression Practice Quiz "Authentication!"

Hey there, I have some Problems at the Python Practice Quiz "Authentication!" the input is a Password which shall has -at least one uppercase character and at least one number I got some help from google to solve it and surprisingly I really passed this Quiz but the Code is not right and I have no Idea how to change it, that it works in the right way. The Problem is: the most Password inputs are recognized correctly (see Examples below) but for example the Password "aA1" which also contains a uppercase character and one number, it shows "wrong format". Thanks for your Help my Code: import re password = input() pattern = r"\d.*[A-Z]|[A-Z].*\d" if re.match(pattern, password): print("Password created") else: print("Wrong format") Example Inputs -> Outputs: Aa1 ->Password created A1a ->Password created 1Aa ->Password created 1aA ->Password created aA1 ->Wrong format -->should be "Password created" a1A ->Wrong format -->should be "Password created"

4th Jan 2021, 6:49 PM
Jochen Müller
Jochen Müller - avatar
21 Answers
4th Jan 2021, 9:16 PM
JaScript
JaScript - avatar
+ 2
Thanks for your replies I thought this is going to be a a easier Quiz because it is more at the beginning of the Regular Expression Chapter. The solutuion from Lisa maybe not that Elegant but at least it worked as it should. This helped me a lot. thanks this is now my code which is working in the right way: import re passwords = ["Aa1", "A1a", "1Aa", "1aA", "a1A", "aA1", "aa1", "aab"] for p in passwords: number = re.search("[0-9]",p) LETTER = re.search("[A-Z]",p) if bool(number) is True and bool(LETTER) is True: print("Password created") else: print("Wrong format") Still curious how the single regex would look like ;)
5th Jan 2021, 10:42 AM
Jochen Müller
Jochen Müller - avatar
+ 2
Calvin Sheen Thomas Yeah that was the regex the OP used and was having issues. I ended up using two expressing numPat = r”[0-9]” upperPat = r”[A-Z]” if re. search(numPat, password) and re.search(upperPat, password) I haven’t sorted a way to get it all in one expression
16th Jan 2021, 2:35 AM
Cody Knight
Cody Knight - avatar
+ 1
It tried around and now my regular expression is: pattern = r"[A-Z0-9]" and it works exactly as my previous!? ( aA1 and a1A ->Wrong format) So now I have a shorter Expression but still the same Problem :D
4th Jan 2021, 8:50 PM
Jochen Müller
Jochen Müller - avatar
+ 1
Seems to be a pro quiz 🤔 Is it necessary to use a single regex? If not we could work around by using one pattern to check for number and one for capital letters (though not very elegant) passwords = ["aA1", "1Aa", "A11", "a1a", "Aaq"] for p in passwords: number = re.search('[0-9]', p) LETTER = re.search('[A-Z]', p) print(p, bool(number), bool(LETTER))
4th Jan 2021, 9:28 PM
Lisa
Lisa - avatar
+ 1
I adapted the example from https://www.tutorialspoint.com/How-to-check-if-a-string-has-at-least-one-letter-and-one-number-in-JUMP_LINK__&&__Python__&&__JUMP_LINK to combi = '^(?=.*[0-9])(?=.*[A-Z])' and then tried re.match()
5th Jan 2021, 12:29 PM
Lisa
Lisa - avatar
+ 1
Hey Jochen, Not sure if you sorted this out yet. Have you tried using re.search() instead of re.match()? re.match() starts at the beginning of the string and will return false if their isn't an immediate match. re.search() looks at entire string for a match. I think pattern = r"[A-Z0-9]" checks for the characters in that order. HAven't figured this out with single regex yet
16th Jan 2021, 12:42 AM
Cody Knight
Cody Knight - avatar
+ 1
Cody Knight Your regex expression doesn't give the required results; it just searches for a string which has an upper-case letter OR an integer.
16th Jan 2021, 2:13 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Yup, doing this with two regex is easy. Here's the single regex version: r"([A-Z]+\w*\d)|(\d+\w*[A-Z])" This works but using two regex is the preferable method for me.
16th Jan 2021, 4:36 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
import re password = input() pattern = r"[A-Z]+[a-z]*[0-9]+" if re.search(pattern, password): print("Password created") else: print("Wrong format")
21st May 2021, 3:24 AM
Wedja Souza
Wedja Souza - avatar
+ 1
import re password = input() #your code goes here at_least_one_uppercase = "[A-Z]+" at_least_one_number = "[0-9]+" if re.search(at_least_one_uppercase, password) and re.search(at_least_one_number, password): print("Password created") else: print("Wrong format")
25th Mar 2022, 7:33 PM
Rusel Bruff
0
Maybe? pattern = r"\d.*[A-Z]|[a-z].\*d"
4th Jan 2021, 6:57 PM
Lisa
Lisa - avatar
0
with this Code Aa1 and A1a also shown as Wrong Format
4th Jan 2021, 8:46 PM
Jochen Müller
Jochen Müller - avatar
0
Here you go: from re import search as a b,c=r"([A-Z]+\w*\d)|(\d+\w*[A-Z])",input() d="Verified" if a(b,c) else "Not Verified" print(d) #Might as well copy and paste it in the #playground.
6th Jan 2021, 10:03 AM
Calvin Thomas
Calvin Thomas - avatar
0
The simplest expression is as following: import re password = input() #your code goes here pattern = r".*[A-Z].*[0-9]" if re.match(pattern, password): print("Password created") else: print("Wrong format") .* means every character except a line break, a word start is also considered in this. Would you change the pattern to pattern = r"([A-Z])(.*[0-9])" then the password must start with an uppercase character. ------ or even shorter with re.search since this function doesn't care about the start of the string: import re password = input() #your code goes here pattern = r"[A-Z].*[0-9]" if re.search(pattern, password): print("Password created") else: print("Wrong format")
23rd Jun 2021, 8:46 AM
Maikel
0
Maikel Your code doesn't work for inputs like "8A" and "234LPP". This pattern seems to work with re.search(): r"[A-Z].*\d|\d.*[A-Z]"
24th Jun 2021, 6:44 AM
Calvin Thomas
Calvin Thomas - avatar
0
You can use this code: import re password = r("Enter Your Pass: ") if re.fullmatch(r'[A-Za-z0-9@#$%^&+=]{8,}', password): print("Password created") else: print("Wrong format")
1st Aug 2021, 4:20 PM
Masoud Ramezani
Masoud Ramezani - avatar
0
import re password = input() #your code goes here pattern = r"[A-Z0-9]+" if re.match(pattern, password): print("Password created") else: print("Wrong format")
18th Aug 2021, 7:54 AM
Anne Jungers
Anne Jungers - avatar
0
If you want to do it as single line, best way is to use lookahead assertion. r"(? =. *[0-9]). *(? =. *[A-Z])" It should be working.
1st Feb 2022, 10:40 AM
Anne-Cathrin Brenner
Anne-Cathrin Brenner - avatar
0
Ah and you don't need the. . * between the paretheses.. Because you have them in the lookaheads. To shorten it: r"(?=.*[0-9])(?=.*[A-Z])"
1st Feb 2022, 10:45 AM
Anne-Cathrin Brenner
Anne-Cathrin Brenner - avatar