PHP正则表达式,用于强大的密码验证 [英] PHP regular expression for strong password validation
问题描述
我在网络上看到了以下正则表达式.
I've seen the following regular expression around the web.
(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$
仅在字符串以下时验证:
It validates only if the string:
* 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屋!