首页 文章

密码的正则表达式必须包含一个大写字符

提问于
浏览
2

我想要一个正则表达式来检查:

密码只包含一个大写字符,包括至少一个特殊字符( #, @, - ),密码长度必须至少为8个符号!

(?=.[A-Z]{1}?)(?=.*[#@-])[A-Za-z#@-]{8,}

如何为密码包含 exactly 一个大写字符?

2 回答

  • 1

    试试这个:

    ^(?=[^A-Z]*[A-Z][^A-Z]*$)(?=.*[#@-])[A-Za-z#@-]{8,}$
    

    演示:https://regex101.com/r/Fr6znT/1

    分解:

    • (?=[^A-Z]*[A-Z][^A-Z]*$) 在该位置意味着:"The whole expression should be 'any number of non uppercase chars', followed by 'one uppercase char', followed by 'any number of non uppercase chars'" .
  • 0

    试试这个:

    ^(?=(?=.*[@#-]{1,})[\S]{8,})([a-z@#-]*[@#-]*[A-Z][a-z@#-]*[@#-]*)$
    

    https://regex101.com/r/v8Erhu/1

相关问题