【发布时间】:2012-01-08 08:00:26
【问题描述】:
我想知道如何使用正则表达式验证密码。
我尝试使用lookahaed 断言,但不知何故失败了。不知道怎么匹配一定长度的字符串。
我的条件是:
- 至少 8 个字符
- 至少一位数
- 至少一个英文小写字母
- 至少一个大写英文字母
- 至少有一个符号!@#$%-
- 只允许使用以上字符
提前致谢!
【问题讨论】:
标签: .net regex validation passwords
我想知道如何使用正则表达式验证密码。
我尝试使用lookahaed 断言,但不知何故失败了。不知道怎么匹配一定长度的字符串。
我的条件是:
提前致谢!
【问题讨论】:
标签: .net regex validation passwords
你在前瞻断言的正确步骤。
您的要求可以细分为:
把它们放在一起,你最终会得到这个:
foundMatch = Regex.IsMatch(subjectString, @"^(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%-])[\da-zA-Z!@#$%-]*$");
这可以反过来解释:
"
^ # Assert position at the beginning of the string
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
{8,} # Between 8 and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\d # Match a single digit 0..9
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[a-z] # Match a single character in the range between “a” and “z”
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[A-Z] # Match a single character in the range between “A” and “Z”
)
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
. # Match any single character that is not a line break character
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
[!@#$%-] # Match a single character present in the list below
# One of the characters “!@#$”
# The character “%”
# The character “-”
)
[\da-zA-Z!@#$%-] # Match a single character present in the list below
# A single digit 0..9
# A character in the range between “a” and “z”
# A character in the range between “A” and “Z”
# One of the characters “!@#$”
# The character “%”
# The character “-”
* # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
【讨论】:
这应该适合你
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%-]).{8,}$
解释
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[!@#$%-] any character of: '!', '@', '#', '$',
'%', '-'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.{8,} any character except \n (at least 8 times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
确实,您不应该强制使用这种密码。请参阅这部值得注意的 xkcd 漫画,了解原因
经过 20 年的努力,我们已成功训练每个人使用人类难以记住但计算机容易猜测的密码。
【讨论】:
您是否有任何理由需要对所有这些规则使用单个正则表达式?单独测试每一个更常见,也更易读。例如:
if(pass.length < 8 || ! pass.matches(/[0-9]/) || ! pass.matches(/[a-z]/)
|| ! pass.matches(/[A-Z]/) || ! pass.matches(/[!@#$%-]/)
|| pass.matches(/[^0-9A-Za-z!@#$%-]/))
{
. . . // reject password
}
【讨论】:
大概是这样的:
(糟糕,最好让这至少正确)
扩展
^
(?=[0-9a-zA-Z!@#$%-]{8,}$)
(?=.*[0-9])
(?=.*[a-z])
(?=.*[A-Z])
(?=.*[!@#$%-])
【讨论】: