Regex para la validación de mi contraseña

I need a regular expression for my password format. It must ensure that password only contains letters a-z, digits 0-9 and special characters: .@#$%&. I am using .NET C# programming language. This is my code:

Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]$");
if (!userAndPassPattern.IsMatch(username) || !userAndPassPattern.IsMatch(password))
    return false;

The problem is that I always get back false.

preguntado el 09 de marzo de 12 a las 13:03

What values are you passing to it? . y $ are regex keywords so they most likely need to be escaped... -

You have the same restriction on usernames too? If not, why are you running the regex against the username? -

Yes I have the same restriction on usernames too. The user can only enter a-z letters, 0-9 digits and those special characters. Should I remove . and $ from the list of characters? -

Did you do a search? This questions has been asked and answered many times. -

Because you seem new. If you think, an answer is correct, click on the tick blow the vore count on the left of the answer. -

5 Respuestas

!A || !B es lógicamente equivalente a !(A && B)

So you could write better

!(userAndPassPattern.IsMatch(username) && userAndPassPattern.IsMatch(password))

Then you have a special character $ in you character class, maybe you need to mask it \$
I'm not quite sure about this, because in a character class it is not a special character. Maybe it depends on the RegEx engine in use. If you mask the $ it should do no harm ([a-z0-9.@#\$%&])

Then you have just a single character to match. You need a quantifier

[a-z0-9.@#$%&] means one single character out of the given, will match aor b or 0 pero no ab
[a-z0-9.@#$%&]+ many characters out of the given, from 1 to endless appearances, will match a, by ab y ba etc.


Esto es lo que quieres

Regex userAndPassPattern = new Regex("^[a-z0-9\.@#\$%&]+$");
if (!(userAndPassPattern.IsMatch(username) && userAndPassPattern.IsMatch(password))) {
    return false;

respondido 09 mar '12, 14:03

Yes, it's true, but I need to check if any of those matches fail. So it will be !(A && B). Thank you. - Rade Milovic

You forgot to add the '+' for matching one or more times:

  Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]+$");

respondido 09 mar '12, 13:03

I have never used Regex before and I didn't have time to learn its syntax.I really appreciate your help. - Rade Milovic

Este código Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]$"); will only match a username or password that is a single character long.

Estas buscando algo como esto Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]+$"); which will match one or more of the characters in your class. The + symbol tells it to match one or more of the previous atom (which in this case is the character class you specified in the square brackets)

Also, if you did not mean to constrain the match to lowercase characters, you should add 'A-Z' to the character class Regex userAndPassPattern = new Regex("^[A-Za-z0-9.@#$%&]$");

You might also want to implement a minimum length restriction which can be accomplished by replacing the + con {n,} construir, donde n is the minimum length you want to match. For example:

this would match a minimum of 6 characters

Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]{6,}$");

this would match a minimum of 6 and a maximum of 12

Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]{6,12}$");

respondido 09 mar '12, 14:03

@Rade Just as an added note, you do not need to escape the . o $ in the character class (this may not be true in every regular expression implementation, but it is true for C#'s) - Gymbrall

You have two problems. First . and $ need to be escaped. Second you are matching only 1 character. Add a + before the last $:


Edit: Another suggestion, if you have a minimum/maximum length you can replace the + with, for example, {6,16} or whatever you think is appropriate. This will match strings that are 6 to 16 character inclusive and reject any shorter or longer strings. If you don't care about an upper limit, you could use {6,}.

respondido 09 mar '12, 14:03

Have you tried using a verbatim string literal when you're using regex escape sequences?

Regex userAndPassPattern = new Regex(@"^[a-z0-9@#\$%&]+$");
if (!userAndPassPattern.IsMatch(username) || !userAndPassPattern.IsMatch(password))
    return false;

Your pattern only allows a single conjunto de caracteres probablemente quieras un operador de repetición como * + or {10,}.

Tus conjunto de caracteres incluye . which matches any character, defeating the object of the character class. If you wanted to match "." then you need to escape it with \.

respondido 09 mar '12, 15:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.