I know that the question has already been answered and accepted, but this is what I would do:
Pattern pattern = Pattern.compile("(?i)(?:((?:\\d+[az]+)|(?:[az]+\\d+))\\w*)"); Object[][] tests = new Object[][] { { "1a1b23nh", Boolean.valueOf(true) }, { "bc112w", Boolean.valueOf(true) }, { "abc", Boolean.valueOf(false) }, { "123", Boolean.valueOf(false) } }; for (Object[] test : tests) { boolean result = pattern.matcher((String)test[0]).matches(); boolean expected = ((Boolean)test[1]).booleanValue(); System.out.print(test[0] + (result ? "\t " : "\t not ") + "accepted"); System.out.println(result != expected ? "\t test failed" : ""); } System.out.println("\nAll checks have been executed");
(? i) makes the regexp case insensitive.
source share