强密码的正则表达式 [英] regular expression for strong password

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

问题描述

我需要一个正则表达式,该正则表达式至少包含以下五个字符类中的两个:

I need a regular expression that Contain at least two of the five following character classes:

  • 小写字母
  • 大写字母
  • 数字
  • 标点
  • 特殊"字符(例如 @#$%^& *()_ + |〜-= \ {} []:;'<>/`等)
  • Lower case characters
  • Upper case characters
  • Numbers
  • Punctuation
  • "Special" characters (e.g. @#$%^&*()_+|~-=\{}[]:";'<>/` etc.)

这是我到目前为止所做的

This is I have done so far

int upperCount = 0;
int lowerCount = 0;
int digitCount = 0;
int symbolCount = 0;

for (int i = 0; i < password.Length; i++)
{
    if (Char.IsUpper(password[i]))
        upperCount++;
    else if (Char.IsLetter(password[i]))
        lowerCount++;
    else if (Char.IsDigit(password[i]))
        digitCount++;
    else if (Char.IsSymbol(password[i]))
        symbolCount++;

但是Char.IsSymbol在@%&上返回false.$.?等.

but Char.IsSymbol is returning false on @ % & $ . ? etc..

并通过正则表达式

Regex Expression = new Regex("({(?=.*[a-z])(?=.*[A-Z]).{8,}}|{(?=.*[A-Z])(?!.*\\s).{8,}})");    
bool test= Expression.IsMatch(txtBoxPass.Text);

但是我需要一个带有"OR"条件的正则表达式.

but I need a single regular expression with "OR" condition.

推荐答案

换句话说,您想要一个不仅仅包含一个类"字符的密码.然后您可以使用

In other words, you want a password that doesn't just contain one "class" of characters. Then you can use

^(?![a-z]*$)(?![A-Z]*$)(?!\d*$)(?!\p{P}*$)(?![^a-zA-Z\d\p{P}]*$).{6,}$

说明:

^           # Start of string
(?![a-z]*$) # Assert that it doesn't just contain lowercase alphas
(?![A-Z]*$) # Assert that it doesn't just contain uppercase alphas
(?!\d*$)    # Assert that it doesn't just contain digits
(?!\p{P}*$) # Assert that it doesn't just contain punctuation
(?![^a-zA-Z\d\p{P}]*$) # or the inverse of the above
.{6,}       # Match at least six characters
$           # End of string

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

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