Regular expression for Password Requirements for PCI Compliance

巧了我就是萌 提交于 2019-12-11 23:58:02

问题


Here are password requirements for PCI compliance:

  • Must contain at least one upper case letter
  • Must contain at least one lower case letter
  • Must contain at least one number
  • Must contain at least one special character such as #, !, ?, ^, or @.

Please tell me how to create such regulat expressions? I can`t figure out how to do it


回答1:


Don't do it as a single regex. There's no need to do it in a single regex, and it will be easier to change the rules, and be much easier to read, if you just make multiple regex checks.

If you were doing this in Perl, for example, you'd just do

my $ok =
    ($pw =~ /[a-z]/) &&  # Has at least one lowercase char
    ($pw =~ /[A-Z]/) &&  # Has at least one uppercase char
    ($pw =~ /\d/)    &&  # Has at least one digit
    ($pw =~ /[#!?^@]);   # Has punctuation

That is far easier to read later on when you have to maintain the code in the future.




回答2:


^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[#\!\?\^@])$

the special characters are individually specified




回答3:


^.*\d.*[a-z].*[A-Z].*[#\!\?\^@].*$

Simply this should do it for you.



来源:https://stackoverflow.com/questions/29632747/regular-expression-for-password-requirements-for-pci-compliance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!