String patt = "^Q[^u]\\d+\\."; Pattern r = Pattern.compile(patt); for (int i=0; i<argv.length; i++) { Matcher m = r.matcher(argv[i]); boolean found = m.lookingAt(); System.out.println(patt + (found ? " matches " : " doesn't match ") + argv[i]); }