Hao Ban Hao Ban - 2 years ago 100
Python Question

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

python 2.7

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

can match
but when

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

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

Answer Source

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):


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download