Regex for Password Validation
本问题已经有最佳答案,请猛点这里访问。
我正在寻找一个正则表达式来验证以下密码:
- 密码必须至少7个字符,最多50个字符
密码必须包含以下四个类别中的至少三个类别的字符:
- 英文大写字母字符(A–Z)
- 英文小写字母字符(A–Z)
- 以10位为基数(0–9)
- 非字母数字字符(例如,!$$,%)
我的尝试如下:
1 | ^.*(?=.{7,50})(?=.*\d)(?=.*[A-Z]).*$ |
这不会检查特殊字符,我也不知道如何使密码包含以下四个类别中至少三个的字符。
是否有人可以帮助建议正则表达式来验证此密码策略?
描述
要更好地查看图像,可以右键单击图像并选择"在新窗口中查看"。
此regex将执行以下操作
- 要求字符串的长度为7-50个字符
- 允许字符串包含
A-Z 、A-Z 、0-9 和!@#$%^&*()_[\]{},.<>+=- 字符。 - 需要以下任意三种情况中的至少一个字符
- 英文大写字母字符
A–Z 。 - 英文小写字母字符
A–Z 。 - 以10位数字为基数的
0–9 。 - 非字母数字字符
!@#$%^&*()_[]{},.<>+=- 。
例子
现场演示
网址:https://regex101.com/r/jr9cc7/1
样本文本
1 2 3 4 5 6 7 8 9 | 1 2 3 4 5 6 12345678901234567890123456789012345678901234567890124567890 aaaaAAAA1111 aaaaBBBBBBB AAAAaaaa__ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa! aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!AA |
允许字符串
1 2 3 | aaaaAAAA1111 AAAAaaaa__ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!A |
解释
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | NODE EXPLANATION ---------------------------------------------------------------------- ^ the beginning of a"line" ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [A-Z] any character of: 'A' to 'Z' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [- any character of: '-', '!', '@', !@#$%^&*()_[\] '#', '$', '%', '^', '&', '*', '(', {},.<>+=] ')', '_', '[', '\]', '{', '}', ',', '.', '<', '>', '+', '=' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [a-z] any character of: 'a' to 'z' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [- any character of: '-', '!', '@', !@#$%^&*()_[ '#', '$', '%', '^', '&', '*', '(', \]{},.<>+=] ')', '_', '[', '\]', '{', '}', ',', '.', '<', '>', '+', '=' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [a-z] any character of: 'a' to 'z' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [0-9] any character of: '0' to '9' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- (?= look ahead to see if there is: ---------------------------------------------------------------------- .*? any character except (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- [- any character of: '-', '!', '@', '#', !@#$%^&*()_[\]{} '$', '%', '^', '&', '*', '(', ')', ,.<>+=] '_', '[', '\]', '{', '}', ',', '.', '<', '>', '+', '=' ---------------------------------------------------------------------- ) end of look-ahead ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- [A-Za-z0- any character of: 'A' to 'Z', 'a' to 'z', 9!@#$%^&*()_[\]{},.< '0' to '9', '!', '@', '#', '$', '%', '^', >+=-]{7,50} '&', '*', '(', ')', '_', '[', '\]', '{', '}', ',', '.', '<', '>', '+', '=', '-' (between 7 and 50 times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional , and the end of a "line" ---------------------------------------------------------------------- |
为了在一个regex中添加多个条件,我们使用管道或括号/(regex1)(regex2)(regex2)/或(regex1)(regex2)
每个regex条件都是非常基本的,对于您所请求的每个条件,都有许多示例。
您可以在这里查看:
密码的regex必须包含至少8个字符、至少1个数字以及小写和大写字母和特殊字符。