Regular expression matching alphanumeric only requiring both letters and numbers

java, regex

Solution

Use look ahead assertion.

strValue.matches("^(?=.*[A-Z])(?=.*\\d)[A-Z\\d]+$")

Problem

I want to match an alphanumeric string containing at least one letter and one number. Is there a simple way to combine the following into a single regular expression? ``` strValue.matches("[A-Z0-9]+") && strValue.matches(".*[A-Z].*") && strValue.matches(".*[0-9].*") ```

Original source