正则表达式密码验证 [英] regex password validation

查看:99
本文介绍了正则表达式密码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到它,以便检查字符串中的2位或更多位数?在String上调用匹配项。

How would I get this so it would check for 2 or more digits in the String? Invoking matches on the String s.

s.matches("^[a-zA-Z0-9]{8,}$");


推荐答案

应该这样做......

This should do it...

^(?=.*[0-9].*[0-9])[a-zA-Z0-9]{8,}$

我做的唯一更改是添加(?=。 * [0-9]。* [0-9])这是一个积极的先行,它将尝试查找密码中的前2位数字。如果它满意,那么正则表达式将照常进行。

The only change I made is adding this (?=.*[0-9].*[0-9]) which is a positive lookahead that will try to find the first 2 digits within the password. If it's satisfied, then the regular expression will proceed as usual.

现在,我只是想我会指出你的正则表达式将禁止特殊字符(标点符号等) )。在实践中,有些人喜欢在他们的密码中输入这样的奇怪字符。

Now, I just thought I'd point out that your regular expression will disallow special characters (punctuation and such). In practice, some people like to enter weird characters like this in their passwords.

所以你可能会考虑更像这样的东西......

So you might consider something more like this...

^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{8,}$

这将允许特殊字符,同时还确保至少一个大写字母,一个小写字母,密码中存在一个数字。这只是我写回来的强密码正则表达式的一个例子,如果你愿意的话,你当然可以放松一下这些限制。

This will allow special characters while also ensuring at least one capital letter, one lower case letter, and one number exist in the password. This is just an example of a strong password regular expression I wrote awhile back, and you could certainly relax those restrictions a bit if you so desired.

这篇关于正则表达式密码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆