Regex is not working in C#
I have written a RegEx for password which takes any character with min length of 5 and maximum length of 30.
I've tried the following expression:
(\S){5,30}
but it also accepts passwords with more than 30 characters. How can I make sure it doesn'开发者_高级运维t match such passwords?
Your problem is that your regex also matches substrings of your input.
\S{5}
(or (\S){5}
) matches 12345
in the string 1234567890
.
So you need to anchor your regex:
^\S{5,30}$
validates a 5-30 character, non-whitespace string. The parentheses around \S
are useless and unnecessary.
At any rate, why would you impose a length restriction on a password? And why wouldn't you allow whitespace characters in it? See also this.
If you really want "any character" use the period (.
) instead of \S
.
精彩评论