+ 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
18 Antworten
+ 5
Jayakrishna🇮🇳 ,
thank you for your great explanation. very well done!
+ 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...
+ 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}')
+ 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" )
+ 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..
+ 2
Try this
pattern = r"[A-Z].*\d|\d.*[A-Z]"
+ 2
Cris Tradio ,
the current pattern does accept the inputs e.g.:
'passWord' or 'WXayy'
even though the digits are missing.
+ 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?
+ 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)
+ 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)
+ 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 :}
+ 2
Okay...thank you! Jayakrishna🇮🇳
+ 1
Thank you.
+ 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")
+ 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"
0
And what u wanna to know? Ur code working.
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.