![](/img/trans.png)
[英]Python Password Validation: Unable to use constants from string library in regex
[英]Python | regex | String Validation
import re
u,d,c=0,0,0
n=int(input())
for i in range(0,n):
uid=str(input())
uid = "".join(sorted(uid))
if (len(uid)<=10):
for i in uid:
if(re.search("[a-z]", uid)):
flag=-1
if(re.search("[0-9]", uid)):
flag=-1
if(re.search("[A-Z]", uid)):
flag=-1
if(uid.count(i)>1):
c+=1
if(i.isupper()): #uppercase
u+=1
if(i.isdigit()):
d+=1
if(u>=2 and d>=3 and flag==-1 and c==0):
print("Valid")
else:
print("Invalid")
上面的代碼用於驗證uid(string)
。
當我傳遞 2 個值並且第一個值無效時,它會被正確驗證並打印"invalid"
然后對於下一個值,即使它是有效的,它仍然會打印"invalid"
。 然而,如果第一個值有效,則打印"valid"
,如果下一個值無效,則打印"invalid"
。
添加圖片供參考(圖片中第一個值因重復字符而無效,但第二個值有效,仍然顯示無效)
驗證 uid 的規則:
It must contain at least 3 digits (0-9).
It must contain at least 2 uppercase English alphabet characters.
It should only contain alphanumeric characters (a-z, A-Z & 0-9 ).
No character should repeat.
There must be exactly 10 characters in a valid UID.
您通過嘗試將字符串與以下正則表達式匹配來執行驗證。
^(?=(?:.*\d){3})(?=(?:.*[A-Z]){2})(?!.*(.).*\1)[A-Za-z0-9]{10}$
Python 的正則表達式引擎執行以下操作。
^ : assert beginning of string
(?= : positive lookahead to assert string contains at
least three digits
(?:.*\d) : match 0+ chars, then 1 digit in a non-capture group
{3} : execute non-capture group thrice
) : end positive-lookahead
(?= : positive lookahead to assert string contains at
least two capital letters
(?:.*[A-Z]) : match 0+ chars, then 1 uppercase letter in a
non-capture group
{2} : execute non-capture group twice
) : end positive-lookahead
(?! : negative lookahead to assert string does contain
the same character twice
.*(.).*\1 : match 0+ chars, 1 character saved to capture group
1, 0+ chars, contents of capture group 1
) : end negative lookahead
[A-Za-z0-9]{10} : match 10 letters or digits
$ : assert end of string
請注意,肯定的前瞻用於斷言字符串包含“某物”(這里,至少 3 個數字和至少 2 個大寫字母)。 否定前瞻用於斷言字符串不包含“某物”(此處為重復的字符)。 [A-Za-z0-9]{10}
與^
和$
錨點一起斷言字符串中允許的字符和字符串的長度。
@Thefourthbird 在評論中建議了此正則表達式的更有效變體:
^(?=(?:[^\d\s]*\d){3})(?=(?:[^A-Z\s]*[A-Z]){2})(?!.*?(.).*\1)[A-Za-z0-9]{10}$
如所見,該正則表達式需要 596 個步驟用於測試字符串。 這與我提出的正則表達式所需的 906 相比。
您已將變量u
、 d
、 c
為全局變量。 這就是為什么在您的循環的下一次迭代中,它們的值已經被修改,因此您面臨來自循環的正確 output 的問題。
只需將變量保留在循環中,您的 output 就會正確。
import re
n = int(input())
for i in range(0, n):
u, d, c, flag = 0, 0, 0, 0 # either move the variables inside the loop or reset them everytime
uid = str(input())
uid = ''.join(sorted(uid))
if len(uid) <= 10:
for i in uid:
if re.search('[a-z]', uid):
flag = -1
if re.search('[0-9]', uid):
flag = -1
if re.search('[A-Z]', uid):
flag = -1
if uid.count(i) > 1:
c += 1
if i.isupper(): # uppercase
u += 1
if i.isdigit():
d += 1
if u >= 2 and d >= 3 and flag == -1 and c == 0:
print ('Valid')
else:
print ('Invalid')
OUTPUT:
2
B1CD102354
Invalid
B1CD602354
Valid
看起來您永遠不會在循環的每次迭代之間重置變量。
import re
n=int(input())
for i in range(0,n):
u,d,c,flag=0,0,0 # move this inside the loop
uid=str(input())
uid = "".join(sorted(uid))
if (len(uid)<=10):
for i in uid:
if(re.search("[a-z]", uid)):
flag=-1
if(re.search("[0-9]", uid)):
flag=-1
if(re.search("[A-Z]", uid)):
flag=-1
if(uid.count(i)>1):
c+=1
if(i.isupper()): #uppercase
u+=1
if(i.isdigit()):
d+=1
if(u>=2 and d>=3 and flag==-1 and c==0):
print("Valid")
else:
print("Invalid")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.