Pattern p = Pattern.compile("[a-zA-Z]|([0-9]{1,}|\\_){6,20}"); Matcher m = p.matcher("a1b2c3d4e45"); while (m.find()){ System.out.println("ok"); }