基本上java所有的正则表达式都可以需要转义
一丶基础 d表示数字 如果我们只想匹配0~9这样的数字,可以用d匹配String regex = "\d\d\d\d";
System.out.println("2222".matches(regex));
System.out.println("2A22".matches(regex));
.表示所有通配符
String re3 = "a.c"; // 对应的正则是a&c
System.out.println("a&c".matches(re3));
System.out.println("a-c".matches(re3));
System.out.println("a&&c".matches(re3));
System.out.println("a@c".matches(re3));
System.out.println("a_c".matches(re3));
System.out.println("a c".matches(re3));
w可以匹配一个字母、数字或下划线,w的意思是word
String re3 = "a\wc";
System.out.println("a&c".matches(re3));
System.out.println("a-c".matches(re3));
System.out.println("a&&c".matches(re3));
System.out.println("a@c".matches(re3));
System.out.println("a c".matches(re3));
System.out.println("aac".matches(re3));
System.out.println("a2c".matches(re3));
System.out.println("a_c".matches(re3));
s可以匹配一个空格字符,注意空格字符不但包括空格,还包括tab字符(在Java中用t表示)。例如,asc可以匹配
用d可以匹配一个数字,而D则匹配一个非数字
重复匹配
修饰符可以匹配任意个字符,包括0个字符。我们用Ad可以匹配:
A:因为d可以匹配0个数字;
A0:因为d可以匹配1个数字0;
A380:因为d*可以匹配多个数字380。
A0:因为d+可以匹配1个数字0;
A380:因为d+可以匹配多个数字380。
但它无法匹配"A",因为修饰符+要求至少一个字符。
A:因为d?可以匹配0个数字;
A0:因为d?可以匹配1个数字0。
但它无法匹配"A33",因为修饰符?超过1个字符就不能匹配了。
A380:因为d{3}可以匹配3个数字380。
修饰符{n,m}就可以。Ad{3,5}可以精确匹配:A380:因为d{3,5}可以匹配3个数字380;
A3800:因为d{3,5}可以匹配4个数字3800;
A38000:因为d{3,5}可以匹配5个数字38000。
如果没有上限,那么修饰符{n,}就可以匹配至少n个字符。



