python正则表达式re.search(r“([az] + [AZ] + [0-9] +)”,密码)(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>可以匹配但什么时候
>>>password="Fa11con77YES" >>>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!
最满意答案
如果您要确保密码中至少有一个(较低,较高,数字),则需要单独检查:
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))基本正则表达式是特定于订单的。 另一种方法是使用前瞻(如果你的正则表达式支持它):
re.search(r"(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])")然而,这可能不如仅仅独立地执行每个检查。
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.
更多推荐
发布评论