Reputation: 1015
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.
Upvotes: 0
Views: 2623
Reputation: 6136
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 character set you probably want a repetition operator like *
+
or {10,}
.
Your character set includes .
which matches any character, defeating the object of the character class. If you wanted to match "."
then you need to escape it with \.
Upvotes: 0
Reputation: 33449
!A || !B
is logically equivalent to !(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 a
or b
or 0
but not ab
[a-z0-9.@#$%&]+
many characters out of the given, from 1 to endless appearances, will match a
, b
, and ab
and ba
etc.
This is what you want
Regex userAndPassPattern = new Regex("^[a-z0-9\.@#\$%&]+$");
if (!(userAndPassPattern.IsMatch(username) && userAndPassPattern.IsMatch(password))) {
return false;
}
Upvotes: 3
Reputation: 45155
You have two problems. First . and $ need to be escaped. Second you are matching only 1 character. Add a + before the last $:
^[a-z0-9\.@#\$%&]+$
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,}.
Upvotes: 1
Reputation: 2063
This code Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]$");
will only match a username or password that is a single character long.
You are looking for something like this 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 +
with the {n,}
construct, where 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}$");
Upvotes: 1
Reputation: 12786
You forgot to add the '+' for matching one or more times:
Regex userAndPassPattern = new Regex("^[a-z0-9.@#$%&]+$");
Upvotes: 2