简体   繁体   English

python正则表达式re.search(r“([[az] + [AZ] + [0-9] +)”,密码)

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

python 2.7 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. 您的正则表达式描述的字符串包含1个或多个小写字符,后跟1个或多个大写字符,后跟一个或多个数字。

In the first case (ULFFunH8ni) it finds "unH8"; 在第一种情况(ULFFunH8ni)中,它找到的是“ unH8”;

In the second case (Fa11con77YES) there is no substring that matches. 在第二种情况下(Fa11con77YES),没有匹配的子字符串。

If you want the entire string to match the regular expression you should use re.match(); 如果希望整个字符串与正则表达式匹配,则应使用re.match();。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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