Java RegEx – Match at least one lowercase, uppercase, special character example shows how to match at least one uppercase, lowercase, or special character in a string using regex in Java.
How to match at least one from the character class using regex in Java?
Many a time we want to check if the string contains any upper case character, lower case character, a special character from the provided list, or a digit between 0 to 9. Usually, we want to do that when we want to validate a username or validate a password using regex.
If you want to match only one condition like the string must have at least one uppercase character, it can be easily done by the below-given pattern.
1 |
^.*[A-Z].*$ |
It means anything followed by any one character from A-Z followed by anything, thus it matches with the string that has any one of the uppercase characters from A to Z.
1 2 3 |
System.out.println( "abcd".matches("^.*[A-Z].*$") ); System.out.println( "abc123".matches("^.*[A-Z].*$") ); System.out.println( "Abc".matches("^.*[A-Z].*$") ); |
Output
1 2 3 |
false false true |
You can change the pattern to suits your needs, for example, character range a-z is used to check a lowercase character. Similarly, you can use 0-9 to check for any digit in the string.
However, this kind of pattern does not work when we want to check for multiple conditions like at least one uppercase and one lowercase letter. Have a look at the below given example to understand that.
1 2 3 |
//means any char between A-Z or a-z System.out.println( "abc".matches("^.*[A-Za-z].*$") ); System.out.println( "ABC".matches("^.*[A-Za-z].*$") ); |
Output
1 2 |
true true |
The pattern matched even though both of the strings contained characters either in upper or lower case only. It is because the specified pattern means any character from a to z OR A to Z. Presence of any one of them makes the match true.
Even if we define separate character classes, it does not work. Have a look at these statements.
1 2 3 |
//means any char from a-z followed by any char between A-Z System.out.println( "Aa".matches("^.*[A-Z].*[a-z].*$") ); System.out.println( "aA".matches("^.*[A-Z].*[a-z].*$") ); |
Output
1 2 |
true false |
The reason the second string did not match with the pattern even though it had both upper and lower case characters is that the regex engine consumes characters while matching them with the pattern. It consumed the second character while matching the part [A-Z] part, it cannot go back to the first character from there.
In order to avoid this, we need to use the positive lookahead expression. As the positive lookahead name implies, it does not consume any characters, it just looks ahead to the right from the current position to see for any matches. A simple positive lookahead expression to check if there is at least one digit in the string will be like given below.
1 |
^(?=.*\\d).*$ |
It means,
1 2 3 |
^ - Start of the string (?=.*\\d) - any character followed by any digit look ahead $ - End of the string |
Basically, it checks for anything that is followed by a digit in the string, thus confirming the existence of a digit.
1 2 3 4 |
System.out.println( "abc".matches("(?=.*\\d).*") ); System.out.println( "123".matches("(?=.*\\d).*") ); System.out.println( "a1".matches("(?=.*\\d).*") ); System.out.println( "1a".matches("(?=.*\\d).*") ); |
Output
1 2 3 4 |
false true true true |
Now if you want to combine multiple lookups, you can do so by combining the pattern that checks a particular class of characters as given below. For example, the below-given pattern checks for at least one uppercase, one lowercase, and one digit in the string.
1 |
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$ |
Let’s try that out with a few sample strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//false, only one character class System.out.println( "a".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "B".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "1".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); //false, only two character classes System.out.println( "aB".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "a1".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "B1".matches("^(=?.*[a-z])(=?.*[A-Z])(?=.*[0-9]).*$") ); //true, all three are present at least one time System.out.println( "123Password".matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "HelloWorld123".matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$") ); System.out.println( "password123Abc".matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$") ); |
Output
1 2 3 4 5 6 7 8 9 |
false false false false false false true true true |
As you can see from the output, it only matches when all of the three character classes are present in the string. You add the check for at least one special character in the pattern if you want. If you are looking for validating a password, visit the Java regex validate password example.
If you want to learn more about the regex patterns, please visit the Java Regex tutorial.
Please let me know your views in the comments section below.
your website is very helpful for me