System.out.println("Hello49032432".matches("H\\w{4}\\d+")); //true
严格匹配
System.out.println("China".matches("China")); //true
System.out.println("A".matches("."));//true System.out.println("\n".matches("."));//false System.out.println("AA".matches("."));//false System.out.println("AA".matches(".A"));//true //要匹配.字符本身,请使用\. System.out.println(".".matches("\\."));//true
//匹配出现n次 System.out.println("aaaaaaa".matches("a{7}"));//true System.out.println("aaaaaaa".matches(".{7}"));//true{7}表示前面的字符出现了7次,但是像一个问题,如果我们就是要匹配.或者是{
//匹配.{本身 System.out.println("aaa{{{...".matches("a{3}\\{{3}.{3}"));//true
//匹配N次以上 System.out.println("aaaaa".matches("a{3,}"));//true
//匹配3(含)到5(含)次之间 System.out.println("aaa".matches("a{3,5}"));//true
星号:*
//匹配0次至多次 System.out.println("".matches("a*"));//true
字符+
//匹配1到多次 System.out.println("a".matches("a+"));//true
问号:?
//匹配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("a34567".matches("a\\d+")); //匹配非数字 System.out.println("a34567".matches("\\D\\d+"));
//匹配空白字符。包括空格、制表符、回车符、换页符、换行符等\s System.out.println(("abc \n012").matches("\\D{3}\\s+\\d{3}")); //true
//匹配所有的非空白字符\S System.out.println(("abc \n012").matches("\\S+\\s+\\S+")); //true
//匹配任意数字字母下划线 System.out.println("123fdafadsHKJHK___".matches("\\w+"));//true System.out.println("fdsafds...~~~".matches("\\w+.+~+"));//true
//匹配非单词字符 System.out.println("%%%&&**((^^%$##@@!!---+++=== \n\n".matches("\\W+"));//true System.out.println("台湾人是中国人".matches("\\W+"));//true
//匹配[]内任意一个字符 System.out.println("a".matches("[abdc]"));//true
//匹配范围单个字符 System.out.println("dfafjiewjhrlhjl4354356434ABC".matches("[a-z0-9A-Z]+")); //true
//匹配不在范围[^a-z],[^0-9] System.out.println("A".matches("[^a-z]")); //true
//匹配同时计算 System.out.println("a".matches("[a-z&&[def]]")); //false System.out.println("e".matches("[a-z&&[def]]")); //true
//或者关系 System.out.println("112312".matches("[a-z] |\\d+")); //true
//边界匹配,以什么开头^,以什么结尾$ System.out.println("helloiuowjklkdshku49239784093280end".matches("^he[a-z0-9]+end$")); //true