0

I have to validate strings with specific conditions using a regex statement. The conditions are that the string has an unlimited amount of characters, at least one uppercase letter, at least one digit and no whitespaces.

So, I wrote a statement which filters a string according to those conditions and prints true once a string fullfies everything, however it only seems to print "false" altough some string met those conditions.

public class MyClass {
    public static void main(String args[]) {
     
    String[] values = {"Abcde1", "1a2B3c", "000OOO", " 000OOO", "1a2b3c", "abcdeF"};
        for (String s : values){
            System.out.println("\"" + s + "\"" + " -> " + validate(s));
        }
    }
    
    public static boolean validate(String s){
        return s.matches("([A-Z]+)([^\\s])([0-9]+)");
    }
}

0 Answers0