1. 程式人生 > 其它 >python 正則匹配

python 正則匹配

正則匹配

  1. 題目描述:

現公司要開發一個業務管理系統,要求註冊環節的密碼需要提示使用者其安全等級,密碼按如下規則進行計分,並根據不同的得分為密碼進行安全等級劃分;此外,密碼的組成可以由字母,數字,以及符號構成。

以下為密碼分數判斷選項,每項判斷只能拿一個分項:

  • 密碼長度:

    • 5 分: 小於等於4個字元
    • 10 分: 5到7字元
    • 25 分: 大於等於8個字元
  • 字母:

    • 0 分: 沒有字母
    • 10 分: 全都是小(大)寫字母
    • 20 分: 大小寫混合字母
  • 數字:

    • 0 分: 沒有數字
    • 10 分: 1個數字
    • 20 分: 大於1個數字
  • 符號:

    • 0 分: 沒有符號
    • 10 分: 1個符號
    • 25 分: 大於1個符號
  • 獎勵:

    • 2 分: 字母和數字
    • 3 分: 字母、數字和符號
    • 5 分: 大小寫字母、數字和符號

最後的評分標準:

大於等於90: 非常安全
大於等於80: 安全
大於等於70: 非常強
大於等於 60: 強
大於等於 50: 一般
大於等於 25: 弱
大於等於0: 非常弱

當用戶輸入一個密碼字串後,判斷密碼等級,並輸出

輸入描述:

1
b12A3%$123

輸出:

1
非常安全

程式碼實現如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import re


def s1(password):
if re.match(r".{,4}$", password):
return 5
if re.match(r".{5,7}$", password):
return 10

if re.match(r".{8,}$", password):
return 25


def s2(password):
if re.match(r"[^a-zA-Z]*$", password):
return 0
if re.match(r"([a-z]*$)|([A-Z]*$)", password):
return 10

if re.match(r".*(?=.*[a-z])(?=.*[A-Z]).*$", password):
return 20


def s3(password):
if re.match(r"[^\d]*$", password):
return 0
if re.match(r"[^\d]*[\d][^\d]*$", password):
return 10
if re.match(r".*[\d]+.*[\d]+.*$", password):
return 20


def s4(password):
if re.match(r"[^!@#$%^&*?]*$", password):
return 0
if re.match(r"[^!@#$%^&*?]*[!@#$%^&*?][^!@#$%^&*?]*$", password):
return 10
if re.match(r".*[!@#$%^&*?]+.*[!@#$%^&*?]+.*$", password):
return 20


def s5(password):
if re.match(r".*(?=.*\d.*$)(?=.*[!@#$%^&*?].*$)(?=.*[a-z].*$)(?=.*[A-Z].*$).*$", password):
return 5

if re.match(r"(?=.*\d.*$)(?=.*[!@#$%^&*?].*$)(?=.*[a-zA-Z].*$).*$", password):
return 3

if re.match(r".*(?=.*[\d].*$)(?=.*[a-zA-Z].*$).*$", password):
return 2


password = input("請輸入密碼:")
ret = sum((s1(password), s2(password), s3(password), s4(password), s5(password)))

if ret >= 90:
print("非常安全")
elif ret >= 80:
print("安全")
elif ret >= 70:
print("非常強")
elif ret >= 60:
print("強")
elif ret >= 50:
print("一般")
elif ret >= 25:
print("弱")
else:
print("非常弱")