Skip to content

Java RegEx – Match at least one lowercase uppercase or special character

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.

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.


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.


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.


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.

It means,

Basically, it checks for anything that is followed by a digit in the string, thus confirming the existence of a digit.


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.

Let’s try that out with a few sample strings.


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.

About the author


Leave a Reply

Your email address will not be published.