反过来判断如果符合“不存在数字字母,或者只有其中一种的”就是错误的    String regex = "[^\\p{Alnum}]*([\\d]*|[\\p{Lower}]*|[\\p{Upper}]*)[^\\p{Alnum}]*";
    // NG
    System.out.println("---NG---");
    System.out.println(!"abc".matches(regex));
    System.out.println(!"ABC".matches(regex));
    System.out.println(!"123".matches(regex));
    System.out.println(!";, \t".matches(regex));
    System.out.println(!";,ABC \t".matches(regex));
    System.out.println(!";,abc \t".matches(regex));
    System.out.println(!";,123 \t".matches(regex));
    // OK
    System.out.println("---OK---");
    System.out.println(!"abc123".matches(regex));
    System.out.println(!"ABC123".matches(regex));
    System.out.println(!"abcAbc".matches(regex));
    System.out.println(!";,abcABC \t".matches(regex));
    System.out.println(!";,ABCabc \t".matches(regex));
    System.out.println(!";,123ABC \t".matches(regex));
    System.out.println(!";,123abc \t".matches(regex));
    System.out.println(!";,abc123 \t".matches(regex));
    System.out.println(!";,ABC123 \t".matches(regex));
    System.out.println(!";,abc123ABC \t".matches(regex));
    System.out.println(!";,ab12c ... A34Bc \t".matches(regex));

解决方案 »

  1.   

    更正一下,刚才的错了    String regex = "[\\d[^\\p{Alnum}]]*|[\\p{Lower}[^\\p{Alnum}]]*|[\\p{Upper}[^\\p{Alnum}]]*";
        // NG
        System.out.println("---NG---");
        System.out.println(!"abc".matches(regex));
        System.out.println(!"ABC".matches(regex));
        System.out.println(!"123".matches(regex));
        System.out.println(!";, \t".matches(regex));
        System.out.println(!";,ABC \t".matches(regex));
        System.out.println(!";,abc \t".matches(regex));
        System.out.println(!";,123 \t".matches(regex));
        // 以下几种情况楼上的没有考虑到
        System.out.println(!"a b".matches(regex));
        System.out.println(!"A B".matches(regex));
        System.out.println(!"1 2".matches(regex));
        System.out.println(!";,A;;;BC \t".matches(regex));
        System.out.println(!";,a;;;bc \t".matches(regex));
        System.out.println(!";,1;;;23 \t".matches(regex));
        // OK
        System.out.println("---OK---");
        System.out.println(!"abc123".matches(regex));
        System.out.println(!"ABC123".matches(regex));
        System.out.println(!"abcAbc".matches(regex));
        System.out.println(!";,abcABC \t".matches(regex));
        System.out.println(!";,ABCabc \t".matches(regex));
        System.out.println(!";,123ABC \t".matches(regex));
        System.out.println(!";,123abc \t".matches(regex));
        System.out.println(!";,abc123 \t".matches(regex));
        System.out.println(!";,ABC123 \t".matches(regex));
        System.out.println(!";,abc123ABC \t".matches(regex));
        System.out.println(!";,ab12c ... A34Bc \t".matches(regex));