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}) matches12345in the string1234567890.So you need to anchor your regex:
validates a 5-30 character, non-whitespace string. The parentheses around
\Sare 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.