Regarding RegEx... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Regarding RegEx...

Hi, can you please help. I created a code to check the inputted password if it will pass the given condition which is to contain at least 1 uppercase letter and at least 1 number in any order. If the condition is met, it will print "password created" otherwise "wrong format", the code has a bug which prints "password created" even though I only inputted 1 uppercase letter. Thanks. https://code.sololearn.com/cvxv980LXKqi/?ref=app

14th Sep 2022, 4:15 PM
Cris Tradio
Cris Tradio - avatar
18 Answers
+ 5
Jayakrishna🇮🇳 , thank you for your great explanation. very well done!
16th Sep 2022, 6:21 AM
Lothar
Lothar - avatar
+ 3
Sandeep Task need a digit \d and Capital letter [A-B] , for these before and after anything so 2 possibilities: digit first then letter, or letter first then digit. 2 classes we need to define: 1) digit before Letter (.*\d+.*[A-Z]+.*) ( .* before, after, between tells -> there can be any characters (0 Or more) may present) 2) CapsLetter before digit (.*[A-Z].*\d.*) There 2 classes can be added by | operator, so that can match anyone class. .* means 0 or more any characters. \d+ one or more digits, we just need to check single digit so \d is enough.. Combining this and simplified solution is : (.*\d.*[A-Z]+.*)|(.*[A-Z].*\d.*) Further it can be simplified to @Ani jona mentioned solution: r"[A-Z].*\d|\d.*[A-Z]" (It represents : A capital letter next a digit anywhere in string or A digit, next A capital Letter anywhere in string, include of in between .* means there may not need consecutively ). Hope it helps...
15th Sep 2022, 8:26 PM
Jayakrishna 🇮🇳
+ 3
the complete task including counting of specific characters can also be done in a simple way with only very basic regex: import re string = "GreenLagune24" upp = len(re.findall(r"[A-Z]", string)) dig = len(re.findall(r"[0-9]", string)) print(f'upper case: {upp}, digits: {dig}')
16th Sep 2022, 6:37 AM
Lothar
Lothar - avatar
+ 3
Sandeep \d represents for digit. \d{2, } represents atleast 2digits sequentially.. (.*\d){2, } represents a class of pattern which matches for atleast 2 digits which may include any number of characters before it.. So it matches : 2asd3, 23, abc23, 2ab3c..., There may be exist simplest forms for patterns also, am not aware of those now... As Lothar mentioned, you can use r"[0-9]" , and findall returns number of matches Use it as if dig >= 2 : print( "passed" )
16th Sep 2022, 8:58 AM
Jayakrishna 🇮🇳
+ 2
\d* means "digit 0 or more", so it accepts 0 digits.. Use \d+ => 1 or more, instead again you need distinguish to classes for any order..
14th Sep 2022, 8:08 PM
Jayakrishna 🇮🇳
+ 2
Try this pattern = r"[A-Z].*\d|\d.*[A-Z]"
15th Sep 2022, 7:18 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Cris Tradio , the current pattern does accept the inputs e.g.: 'passWord' or 'WXayy' even though the digits are missing.
15th Sep 2022, 2:24 PM
Lothar
Lothar - avatar
+ 2
Lothar Right! and as mentioned by Jayakrishna🇮🇳 changing the * could do the trick but there is still one problem, If i remember correctly, regex matches are done from left to right, therefore OP regex pattern has to be in order - words , digits, uppercase, words, digits This means if we input digit first, regex match will fail (like in Samialloh code) Ex: 13aasJHAM Above will fail even though it is a right pattern! Lothar and Jayakrishna, can you tell me how to do this correctly?
15th Sep 2022, 6:07 PM
Sandeep
Sandeep - avatar
+ 2
thanks! Jayakrishna🇮🇳 :) it looks really simple now! You explained it really well 🙌 .. I can't see Ani Jona answer, looks like some new Sololearn bug(i am getting lots of bugs these days like no-notifs) Also, Is there a way to add length checking to current regex? (ex- atleast 2 uppercase or 2 digits and they don't have to be side-by-side)
16th Sep 2022, 3:28 AM
Sandeep
Sandeep - avatar
+ 2
Lothar thanks, I think you misunderstood what I meant, what I want is something like a password validator, not counting the length of a uppercase or digits! For ex: "nan2C" string passes the test, but I want the string to include atleast 2 uppercase and 2 digits. https://code.sololearn.com/cFUWpQ86ikrk/?ref=app Therefore, "nan2C" input should result in "Not Valid" password and something like "NaN22" results in valid password! (because it contains two [A-B] and two \d)
16th Sep 2022, 7:46 AM
Sandeep
Sandeep - avatar
+ 2
Jayakrishna🇮🇳 Lothar it's working! 😁 https://code.sololearn.com/cN05JXMym1Bt/?ref=app Athough it looks a little too big but I think I can modify this from here. I will also try to add the method mentioned by Lothar to make it more clear! thanks again! both of you :}
16th Sep 2022, 9:35 AM
Sandeep
Sandeep - avatar
+ 2
Okay...thank you! Jayakrishna🇮🇳
19th Sep 2022, 1:31 AM
Cris Tradio
Cris Tradio - avatar
+ 1
Thank you.
15th Sep 2022, 12:23 AM
Cris Tradio
Cris Tradio - avatar
+ 1
Hi, thank you all. Below is my revised coding. Any suggestions on improving the way I code.? Open for suggestions... # pw authentication, at least 1 uppercase letter and at least 1 number in any order. import re pw = input() pattern = r"\w*\d*([0-9]+\w*\d*[A-Z]+)|([A-Z]+\w*\d*[0-9]+)\w*\d*" if re.findall(pattern, pw): print("Password created") else: print("Wrong format")
18th Sep 2022, 1:32 PM
Cris Tradio
Cris Tradio - avatar
+ 1
Cris Tradio Here, \w*\d* works same as \w* Since you are using findall, no need to pattern for entire string. Pattern part of string is enough so Can be reduced to... This is enough, pattern = r"\d.*[A-Z]|[A-Z].*\d"
18th Sep 2022, 3:32 PM
Jayakrishna 🇮🇳
0
And what u wanna to know? Ur code working.
14th Sep 2022, 4:29 PM
Samuel Hayden
Samuel Hayden - avatar
0
Yes it's working, what I want to know is my pattern correct?, because I wanted to meet the condition that it should take at least 1 uppercase letter and at least 1 number, but when I inputted uppercase letter/s only without a number, the output says "password created". It should have been "wrong format", because it needs at least 1 uppercase letter and at least 1 number. I don't know if the condition I wanted to meet is doable. Thanks for your reply.
14th Sep 2022, 5:11 PM
Cris Tradio
Cris Tradio - avatar