Regex to check to string containing whitespace & special chars
java, regex
Solution
If you only want to reject whitespaces, you can use:
(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])\\S{6,12}
`\\S` will match only non-whitespaces.
Also, you probably don't need the outer parentheses.
EDIT: Didn't see the other parts.
If you want to reject special characters, you can change the `\\S` to a negated class. Using this though would mean you'll have to use: `[^\\s ... ]`
For example, let's say there shouldn't be any `?` or whitespace. This becomes:
(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[^\\s?]{6,12}
If you don't want `!` either:
(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[^\\s?!]{6,12}
Problem
I have the following regex statement: ``` String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,12})"; ``` And I check the statement like so: ``` Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); if(!matcher.matches()) { // error } ``` How can I adjust this pattern to include checking for any whitespace and special characters? As in the matches() method should fail if the password contains any whitespace or any special characters.