python正则表达式搜索(r“([a-z]+[a-z]+[a-z]+[0-9]+)”,密码)

时间:2021-03-26 22:36:57

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!

无法匹配,我不知道为什么,有人能帮我吗?谢谢!

3 个解决方案

#1


1  

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

基本order-specific regex。另一种方法是使用lookaheads(如果你的regex味道支持它):

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

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

然而,这可能比独立完成每一项检查的效率要低。

#2


0  

Change it to :

把它改成:

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

re.search(r”([a - z]+[0 - 9]+[a - z]+)”,密码)

it should match the order of the character as well.

它也应该符合人物的顺序。

#3


0  

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";

在第一个案例中(ulffun8ni),它发现了“精神错乱”;

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();

#1


1  

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

基本order-specific regex。另一种方法是使用lookaheads(如果你的regex味道支持它):

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

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

然而,这可能比独立完成每一项检查的效率要低。

#2


0  

Change it to :

把它改成:

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

re.search(r”([a - z]+[0 - 9]+[a - z]+)”,密码)

it should match the order of the character as well.

它也应该符合人物的顺序。

#3


0  

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";

在第一个案例中(ulffun8ni),它发现了“精神错乱”;

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();