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

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

问题描述

什么是正确的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天全站免登陆