Java regex for Capital letters and numbers only

java, regex, string

Solution

Use `^[A-Z0-9]+$` as matching pattern. but `matches` method matches the whole string, `[A-Z0-9]+` is enough.

Problem

I am trying to do a simple Regex in Java and it's failing for some reason. All I want to do is, validate whether a string contains upper case letters and/or numbers. So ABC1, 111 and ABC would be valid but abC1 would not be. So I tried to do this: ``` if (!e.getId().matches("[A-Z0-9]")) { throw new ValidationException(validationMessage); } ``` I made sure that e.getId() has ABC1 but it still throws the exception. I know it's something really small and silly but i'm unable to figure it out.

Original source