一枚钓、字符匹配
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] 該范圍內(nèi)的字母或數(shù)字");
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
}
}
三褐奥、任意字符肤寝、數(shù)字、空白字符抖僵、單詞字符
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 匹配一個數(shù)字[0-9]");
System.out.println("3".matches("\\d"));// true
System.out.println("a".matches("\\d"));// false
System.out.println("\\D 匹配一個非數(shù)字[^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("$ 行的結(jié)尾");
System.out.println("hello sir".matches(".*ir$"));// true
System.out.println("空白行:一個或多個(空白并且非換行符)開頭,并以換行符結(jié)尾");
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
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者