使用正则表达式强制使用强密码 [英] Enforcing strong password with regular expressions

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

问题描述

我非常坚持使用正则表达式强制执行强密码。我正在尝试实施的RegEx必须符合以下条件的密码:



  • 最小长度为12个字符;
  • 至少三个大写字母;
  • 至少三个小写字母;
  • 至少三位数;
  • 至少三个特殊字符。


密集搜索使我走向RegEx样本,只需要存在一个特定类型的字符。显然,对于大多数用户来说已经足够了,但在这种情况下,我正在向一位特别要求这些标准的客户提供服务。



最好的我可以上来是这样的:

 ^(?=。* [!@#$%^& *()\ -_ = +`〜\ [\] {} |????。])(= * [AZ])(= * [AZ])(= * [0-9]){12,20} $ 



但不幸的是,这使得诸如!A3aaaaaaaaaaaaa / / code>之类的字符串可以轻松通过。我想我需要在表达式的某个地方使用 {3,} ,但我无法绕过它。任何人都可以伸出援手吗?

解决方案

%^& *()\ -_ = +`〜\ [\] {}?|]) (?=。* [AZ])(?=。* [AZ])(?=。* [0-9])。{12,20}



但不幸的是,这让!A3aaaaaaaaaaaa 等字符串轻松过关。我想我需要在表达式的某个地方使用 {3,} ,但我无法绕过它。任何人都可以伸出援手吗?


您要搜索的内容不是使用正则表达式的情况。 RE允许您量化一组中可能出现的字符数量,但不包括总数。为你想要做的事情写一个正则表达式是不可能的。

你最好用一个验证函数做这个(如果你需要灵活性,可以插入它)。您可以迭代所有输入字符,并查明所有类别字符的总数是否达到您设置的限制。



问候,< br $>


Manfred


I''m tremendously stuck with implementing a regular expression for enforcing strong passwords. The RegEx I''m trying to implement must match a password with the following criteria:


  • Minimum length of 12 characters;
  • At least three uppercase letters;
  • At least three lowercase letters;
  • At least three digits;
  • At least three special characters.

Intensive searching led me towards RegEx samples that simply require the presence of one character of a specific type. Obviously that''d be enough for most users but in this case I''m delivering to a client who specifically demanded these criteria.

The best I could come up with was this:

^(?=.*[!@#$%^&*()\-_=+`~\[\]{}?|])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{12,20}$


But unfortunately, this lets strings such as !A3aaaaaaaaaaaa pass trough with ease. I think I need to use {3,} somewhere in my expression but I can''t wrap my head around it. Can anyone lend a hand?

解决方案

%^&*()\-_=+`~\[\]{}?|])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{12,20}



But unfortunately, this lets strings such as !A3aaaaaaaaaaaa pass trough with ease. I think I need to use {3,} somewhere in my expression but I can''t wrap my head around it. Can anyone lend a hand?


What you are searching for is not a case for using Regular Expressions. RE''s allow you to quantify how many characters of a set may occur in a row, but not how many in total. Writing a regular expression for what you are trying to do is not possible.
You''re better of doing this with a validating function (make it pluggable if you need the flexibility). You can iterate over all the input characters and find out if the totals for all your classes of characters amount to the limits you''ve been set.

Regards,

Manfred


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

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