Python Regular Expression Practice Quiz "Authentication!" | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
+ 2

# Python Regular Expression Practice Quiz "Authentication!"

4th Jan 2021, 6:49 PM
Jochen MĆ¼ller
21 Respostas
4th Jan 2021, 9:16 PM
JaScript
+ 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
+ 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
+ 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
+ 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
+ 1
5th Jan 2021, 12:29 PM
Lisa
+ 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
+ 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
+ 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
+ 1
21st May 2021, 3:24 AM
Wedja Souza
+ 1
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
0
with this Code Aa1 and A1a also shown as Wrong Format
4th Jan 2021, 8:46 PM
Jochen MĆ¼ller
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
0
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
0
1st Aug 2021, 4:20 PM
Masoud Ramezani
0