I need a regular expression that Contain at least two of the five following character classes:
@#$%^&*()_+|~-=\
{}[]:";'<>/` 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++;
but Char.IsSymbol is returning false on @ % & $ . ? etc..
and through regex
Regex Expression = new Regex("({(?=.*[a-z])(?=.*[A-Z]).{8,}}|{(?=.*[A-Z])(?!.*\\s).{8,}})");
bool test= Expression.IsMatch(txtBoxPass.Text);
but I need a single regular expression with "OR" condition.
?= is a positive lookahead, a type of zero-width assertion. What it's saying is that the captured match must be followed by whatever is within the parentheses but that part isn't captured. Your example means the match needs to be followed by zero or more characters and then a digit (but again that part isn't captured).
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "."
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,}$
Explanation:
^ # 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With