Java正则表达式案例

一、字符匹配

public class TestRegex {
    public static void main(String[] args) {
        System.out.println("a?   0次或1次");
        System.out.println("".matches("a?")); // true
        System.out.println("a".matches("a?")); // true
        System.out.println("aa".matches("a?")); // false
        System.out.println("a{0,1}   0次或1次");
        System.out.println("".matches("a{0,1}")); // true
        System.out.println("a".matches("a{0,1}")); // true
        System.out.println("aa".matches("a{0,1}")); // true

        System.out.println("a*   0次或多次");
        System.out.println("".matches("a*")); // true
        System.out.println("aa".matches("a*")); // true
        System.out.println("a{0,}   0次或多次");
        System.out.println("".matches("a{0,}")); // true
        System.out.println("aa".matches("a{0,}")); // true

        System.out.println("a+   至少1次");
        System.out.println("".matches("a+")); // false
        System.out.println("a".matches("a+")); // true
        System.out.println("aa".matches("a+")); // true
        System.out.println("a{1,}   至少1次");
        System.out.println("".matches("a{1,}")); // false
        System.out.println("a".matches("a{1,}")); // true
        System.out.println("aa".matches("a{1,}")); // true

        System.out.println("a{3}   恰好3次");
        System.out.println("".matches("a{3}")); // false
        System.out.println("a".matches("a{3}")); // false
        System.out.println("aaa".matches("a{3}")); // true
        System.out.println("aaaa".matches("a{3}"));// false

        System.out.println("a{n,m}   至少n次,最多m次");
        System.out.println("1232435463685899".matches("\\d{3,100}"));// true
        System.out.println("192.168.0.aaa"
            .matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));// false
    }
}

二、范围和集合

package demo;

public class TestRegex {
    public static void main(String[] args) {
        System.out.println("[abc]   a、b或c");
        System.out.println("a".matches("[abc]")); // true

        System.out.println("[^abc]   除了a、b、c外的任何字符");
        System.out.println("a".matches("[^abc]")); // false

        System.out.println("[a-zA-Z0-9]   该范围内的字母或数字");
        System.out.println("c".matches("[a-zA-Z0-9]")); // true

        System.out.println("[a-d[m-p]]   a-d或m-p(并集)");
        System.out.println("c".matches("[a-d[m-p]]")); // true

        System.out.println("[a-z]|[A-Z]   a-z或A-Z");
        System.out.println("c".matches("[a-z]|[A-Z]")); // true

        System.out.println("[a-z&&[def]   d、e或f(交集)");
        System.out.println("d".matches("[a-z&&[def]]")); // true
        System.out.println("z".matches("[a-z&&[def]]")); // false

        System.out.println("[a-z&&[^def]]   a-z,除了d、e、f");
        System.out.println("d".matches("[a-z&&[^def]]")); // false
        System.out.println("z".matches("[a-z&&[^def]]")); // true

        System.out.println("[a-z&&[^m-p]]   a到z,但非m-p(差集)");
        System.out.println("a".matches("[a-z&&[^m-p]]")); // true
        System.out.println("m".matches("[a-z&&[^m-p]]")); // false
    }
}

三、任意字符、数字、空白字符、单词字符

package demo;

public class TestRegex {
    public static void main(String[] args) {
        System.out.println(".   任意字符");
        System.out.println("a".matches(".")); // true
        System.out.println("abc".matches("...")); // true

        System.out.println("\\d   匹配一个数字[0-9]");
        System.out.println("3".matches("\\d"));// true
        System.out.println("a".matches("\\d"));// false

        System.out.println("\\D   匹配一个非数字[^0-9]");
        System.out.println("3".matches("\\D"));// false
        System.out.println("a".matches("\\D"));// true

        System.out.println("\\s   一个空白字符[\\t\\n\\x0B\\f\\r]");
        System.out.println(" ".matches("\\s"));// true

        System.out.println("\\S   一个非空白字符[^\\s]");
        System.out.println(" ".matches("\\S"));// false

        System.out.println("\\w   一个单词字符[a-zA-Z_0-9]");
        System.out.println("a".matches("\\w"));// true

        System.out.println("\\W   一个非单词字符[^\\w]");
        System.out.println("a".matches("\\W"));// false
    }
}

四、边界匹配

package demo;

public class TestRegex {
    public static void main(String[] args) {
        System.out.println("^   行的开头");
        System.out.println("hello sir".matches("^h.*"));// true
    
        System.out.println("$   行的结尾");
        System.out.println("hello sir".matches(".*ir$"));// true
    
        System.out.println("空白行:一个或多个(空白并且非换行符)开头,并以换行符结尾");
        System.out.println("  \n".matches("^[\\s&&[^\\n]]*\\n$"));//true
    
        System.out.println("\\b   单词边界");
        System.out.println("hello sir".matches("^h[a-z]{1,3}o\\b.*"));//true
    
        System.out.println("\\B   非单词边界");
        System.out.println("hellosir".matches("^h[a-z]{1,3}o\\B.*"));//true
    }
}

推荐阅读更多精彩内容