python正则表达式re.search(r“([az] + [AZ] + [0

编程入门 行业动态 更新时间:2024-10-24 07:29:54
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.

更多推荐

本文发布于:2023-08-07 00:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   python   search   AZ   az

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!