精确匹配一个字符
精确匹配字符串 abc ,
//精确匹配字符串 "abc"String regexabc = "abc";System.out.println("abc".matches(regexabc));// trueSystem.out.println("ABC".matches(regexabc));// falseSystem.out.println("abC".matches(regexabc));// falseSystem.out.println("abcD".matches(regexabc));// false
匹配任意一个字符
用 .
匹配一个任意字符,
String regexDo = "a.c";System.out.println("abc".matches(regexDo));// trueSystem.out.println("a1c".matches(regexDo));// trueSystem.out.println("a@c".matches(regexDo));// trueSystem.out.println("a_c".matches(regexDo));// trueSystem.out.println("a#c".matches(regexDo));// trueSystem.out.println("ac".matches(regexDo));// false
匹配一个字母、数字或下划线
用 \w
可以匹配一个字母、数字或下划线,写法是 \\w
,
String regexw = "java\\w";System.out.println("javac".matches(regexw));// trueSystem.out.println("javaC".matches(regexw));// trueSystem.out.println("java8".matches(regexw));// trueSystem.out.println("java_".matches(regexw));// trueSystem.out.println("java@".matches(regexw));// falseSystem.out.println("javaEE".matches(regexw));// false