用于密码验证的正则表达式(正则表达式) [英] Regex (regular expression) for password validation

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

问题描述

什么是正确的regex,以满足以下密码条件:

What would be the correct regex, to satisfy the following password criteria:

  • 必须至少包含1个小写字母.
  • 必须至少包含1个大写字母.
  • 必须至少包含1个数字.
  • 必须至少包含1个特殊字符(仅允许使用以下特殊字符:!#%).
  • A-Za-z0-9!#%之外,不得包含任何其他字符(例如,不得包括;).
  • 长度必须在8到32个字符之间.
  • Must include at least 1 lower-case letter.
  • Must include at least 1 upper-case letter.
  • Must include at least 1 number.
  • Must include at least 1 special character (only the following special characters are allowed: !#%).
  • Must NOT include any other characters then A-Za-z0-9!#% (must not include ; for example).
  • Must be from 8 to 32 characters long.

这是我尝试过的方法,但是不起作用:

This is what i tried, but it doesn't work:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]).{8,32}

但是应该是:

^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9!#%]{8,32}

但是无论如何,Unihedron的解决方案还是更好的,只是想为那些将来会阅读此问题的用户提及这一点. :)

But Unihedron's solution is better anyways, just wanted to mention this for the users which will read this question in the future. :)

Unihedron的解决方案(也可以在下面的答案中找到,我为自己复制了它,以防万一他更改(将其更新为更好的版本)在他的答案中):

Unihedron's solution (can also be found in his answer below, i copied it for myself, just in case he changes (updates it to an better version) it in his answer):

^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[!#%])[A-Za-z0-9!#%]{8,32}$

我得到了以下regex:

^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9\!\#\@\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]{8,60}$

再次感谢Unihedron和skamazin.感激!

Thanks again Unihedron and skamazin. Appreciated!

推荐答案

使用此正则表达式:

/^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=[^!#%]*[!#%])[A-Za-z0-9!#%]{8,32}$/

这是一个 regex演示

了解详情:

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

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