Java 正则表达式字符篇

精确匹配一个字符

精确匹配字符串 abc ,

        //精确匹配字符串 "abc"
        String regexabc = "abc";
        System.out.println("abc".matches(regexabc));// true
        System.out.println("ABC".matches(regexabc));// false
        System.out.println("abC".matches(regexabc));// false
        System.out.println("abcD".matches(regexabc));// false

匹配任意一个字符

. 匹配一个任意字符,

        String regexDo = "a.c";
        System.out.println("abc".matches(regexDo));// true
        System.out.println("a1c".matches(regexDo));// true
        System.out.println("a@c".matches(regexDo));// true
        System.out.println("a_c".matches(regexDo));// true
        System.out.println("a#c".matches(regexDo));// true
        System.out.println("ac".matches(regexDo));// false

匹配一个字母、数字或下划线

\w 可以匹配一个字母、数字或下划线,写法是 \\w

        String regexw = "java\\w";
        System.out.println("javac".matches(regexw));// true
        System.out.println("javaC".matches(regexw));// true
        System.out.println("java8".matches(regexw));// true
        System.out.println("java_".matches(regexw));// true
        System.out.println("java@".matches(regexw));// false
        System.out.println("javaEE".matches(regexw));// false

你可能感兴趣的:(正则表达式,regex,Java,java,正则表达式,开发语言)