1

i didn't find another solution for this specific problem, so i hope you guys can help.

I need to test if a given password has 2 or more uppercase letters and i want to use regular expressions to do so. what i have now is this:

if (Regex.IsMatch(passw, @"([A-Z]){2,}")){
            Note += 1;
        }

only regular expression:

@"([A-Z]){2,}"

but with this code the programm only works if there are 2 ore more uppercase letters next to each other.

this works: aAAa

this doesn't: aAaA

but i need the latter as password can be fully random.

i searched around the web about regular expressions, but {2,} seem to be the best quantifier for the job, or am i completly wrong there?

thanks for any tipps :)

Nazzaroth
  • 63
  • 7

2 Answers2

0

This should do the job

@"[A-Z].*[A-Z]"

Here you search for an uppercase letter ([A-Z]), then any string (.*), and then another uppercase letter ([A-Z]). If you want to be able to increase the number of required uppercase letters easily you can use this:

@"([A-Z].*){2,}"

Now you look for an uppercase letter, then any string. And you search for this twice or more.

EarthDragon
  • 755
  • 4
  • 14
0

You can use this code to find count upper case and digit in password:

return password.Length >= z
    && new Regex("[A-Z]").Matches(password).Count >= x
    && new Regex("[0-9]").Matches(password).Count >= y
    ;

i hope to help you.

Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23