且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式来验证密码

更新时间:2022-06-03 22:02:52

这是使用正则表达式非常不方便的典型示例之一,因为您必须预见所有可能的排列,这几乎是不可能的:字符的顺序不受约束,最大长度不受限制。



解决方案比这简单得多:扫描所有循环中的字符和cl将它们全部分为4个(未使用#4,检查最开始的长度)类别。您只需要记住发生的次数。然后,对于每个类别,在多重性上应用您的标准:在#1,#2和#3中,必须大于零,在#5中,应该为零。对于#5,而不是如,列出所有并将其写入一个字符串,以计算在该字符串中找不到字符。



这些方法将帮助您:

http://msdn.microsoft.com/en- us / library / dy85x1sa.aspx [ ^ ](对于规则#5,选中不包含),

http://msdn.microsoft.com/en-us/library/system.char.aspx [ ^ ]:

http://msdn.microsoft.com/en-us/library/d1x97616.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/9s91f3by.aspx [ ^ ]。



-SA
This is one of the typical examples where using Regex would be extremely inconvenient, because you would have to foresee all possible permutation, which is nearly impossible: the order of characters is not constrained and maximum length is not limited.

The solution is much simpler than that: scan all characters in the loop and classify them all into 4 (#4 is not used, check up the length in the very beginning) categories you described. You would need to remember only the number of occurences. Then, for each category, apply your criteria on multiplicity: in #1, #2 and #3, must be greater then zero, in #5, should be zero. For #5, instead of "such as", list all and write it in one string, to calculate that a character is not found in that string.

These methods will help you:
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx[^] (for rule #5, check "not contains"),
http://msdn.microsoft.com/en-us/library/system.char.aspx[^]:
http://msdn.microsoft.com/en-us/library/d1x97616.aspx[^],
http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx[^],
http://msdn.microsoft.com/en-us/library/9s91f3by.aspx[^].

—SA


访问这里....





http://www.mkyong.com/regular-expressions/how-to-validate-password- with-regular-expression / [ ^ ]
visit here....


http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/[^]


您好,我在同一主题的CP上有一篇文章。请看一下:强密码验证 [ ^ ]
Hi, I have a post on CP on the same topic. Please have a look at this: Strong Password Validation[^]