Regular match in any order

I want to check a complex password with a regex.

It must have 1 uppercase 1 and one lowercase letter, and not in a specific order. So, although I'm talking about something like this:

m = re.search(r"([az])([AZ])(\d)", "1Az") print(m.group()) 

But I do not know how to tell him to search in any order. I tried to look on the Internet, but I did not find something interesting, thanks for the help.

+4
source share
2 answers

Perhaps you tried to find a password that checks the regular expression, there are a lot of them on the site;)

However, you can use positive images to do this:

 re.search(r"(?=.*[az])(?=.*[AZ])(?=.*\d)", "1Az") 

And to actually match the string ...

 re.search(r"(?=.*[az])(?=.*[AZ])(?=.*\d).{3}", "1Az") 

And now, to ensure that the password is 3 characters long:

 re.search(r"^(?=.*[az])(?=.*[AZ])(?=.*\d).{3}$", "1Az") 

A positive lookahead (?= ... ) ensures that the expression inside is present in the string being tested. Thus, the string must have a lowercase character ( (?=.*[az]) ), an uppercase character ( (?=.*[az]) ) and a digit ( (?=.*\d) ) for the regular expression for 'pass'.

+7
source

Why not just:

 if (re.search(r"[az]", input) and re.search(r"[AZ]", input) and re.search(r"[0-9]", input)): # pass else # don't pass 
+2
source

All Articles