简体   繁体   中英

python regular expression re.search(r“([a-z]+[A-Z]+[0-9]+)”, password)

python 2.7

>>>import re
>>>password="ULFFunH8ni"
>>>re.search(r"([a-z]+[A-Z]+[0-9]+)", password)
<_sre.SRE_Match object at 0x7ff5ffd075d0>

can match but when

>>>password="Fa11con77YES"
>>>re.search(r"([a-z]+[A-Z]+[0-9]+)", password)
>>>

can't match, I don't know why, can someone help me? thanks!

If you're trying to ensure that the password has at least one of each (lower, upper, number) then you need to do separate checks:

low = re.search(r"[a-z]", password)
up = re.search(r"[A-Z]", password)
num = re.search(r"[0-9]", password)
has_all = all((low, up, num))

Basic regexes are order-specific. Another way of doing this would be to use lookaheads (if your regex flavor supports it):

re.search(r"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])")

However this may be less efficient than just doing each of the checks independently.

Change it to :

re.search(r"([az]+[0-9]+[AZ]+)", password)

it should match the order of the character as well.

Your regular expression describes strings that have 1 or more lower case characters followed by 1 or more upper case characters followed by one or more digits.

In the first case (ULFFunH8ni) it finds "unH8";

In the second case (Fa11con77YES) there is no substring that matches.

If you want the entire string to match the regular expression you should use re.match();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM