用于强密码验证的 PHP 正则表达式 [英] PHP regular expression for strong password validation

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

问题描述

我在网上看到过以下正则表达式.

I've seen the following regular expression around the web.

(?=^.{8,}$)((?=.*d)|(?=.*W+))(?![.
])(?=.*[A-Z])(?=.*[a-z]).*$

它仅在字符串时验证:

   * contain at least (1) upper case letter
   * contain at least (1) lower case letter
   * contain at least (1) number or special character
   * contain at least (8) characters in length

我想知道如何转换这个正则表达式,以便它检查字符串

I'd like to know how to convert this regular expression so that it checks the string to

* contain at least (2) upper case letter
* contain at least (2) lower case letter
* contain at least (2) digits
* contain at least (2) special character
* contain at least (8) characters in length

好吧,如果它包含至少 2 个大写、小写、数字和特殊字符,那么我不会需要 8 个字符的长度.

Well, if it contains at least 2 upper,lower,digits and special characters then I wouldn't need the 8 characters length.

特殊字符包括:

`~!@#$%^&*()_-+=[]|{};:'".,/<>?

`~!@#$%^&*()_-+=[]|{};:'".,/<>?

推荐答案

适应该正则表达式的最佳方法是将其丢弃并编写一些代码.所需的正则表达式会如此冗长和复杂,您将无法在编写后两小时阅读.等价的 PHP 代码会很乏味,但至少你能理解你写的东西.

The best way to adapt that regex is to chuck it out and write some code instead. The required regex would be so long and complicated, you wouldn't be able to read it two hours after you wrote it. The equivalent PHP code will be tedious, but at least you'll be able understand what you wrote.

顺便说一下,这并不是要抨击你.在大多数情况下,正则表达式几乎不适合密码强度验证,但您的要求比平时更复杂,而且不值得.另外,您发布的正则表达式是垃圾.永远不要相信你在网上发现的正则表达式.或者任何代码,就此而言.或者,见鬼,任何.:-/

This isn't meant as a slam on you, by the way. Regexes are just barely suitable for password-strength validation in most cases, but your requirements are more complicated than usual, and it's just not worth it. Also, that regex you posted is crap. Never trust regexes you find floating around the web. Or any code, for that matter. Or, heck, anything. :-/

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

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