正则表达式:
主要作用:
a.匹配
b.切割
c.替换
d.获取
1.反斜杠和转义字符
废话不多说,直接上demo
public static void main(String[] args) { // TODO Auto-generated method stub String phoneNum= "28516885191"; isPhoneNum(phoneNum); } //13800000876 public static void isPhoneNum(String phoneNum){ String regex= "1[34578][0-9]{9}"; String regex2= "1[34578]\\d{9}"; boolean isPhoneNum= phoneNum.matches(regex); System.out.println("is phoneNum? "+ isPhoneNum); }
总结
/*
* 正则表达式匹配数字的规则,可以用[0-9]和 \d来表示,
* 但在java语言中,\在字符串中本身是转义字符, 也就是说\不是字符本身的反斜杠\,而是有特殊意义的(转义)
* 正则中,\d,是将普通反斜杠和d连一块,表示数字 (也就是说,正则中的\不是转义字符)。所以java语言中,我们不能直接用\d来表示数字,
* 需要这样:\\d 前一个反斜杠,是转义字符,将第二个反斜杠转义字符转译成 普通的字符反斜杠
*
* */
组的使用及切割:
public static void main(String[] args) { // TODO Auto-generated method stub String string= "andittttbobmmmmmmmmmmmcyanxxdenim"; String regex= "(.)\\1+"; String[] strings= string.split(regex); for (String string2 : strings) { System.out.println("name: "+ string2 ); } }
output:
name: andi
name: bob
name: cyan
name: denim
总结:
在表达式 ((A)(B(C))) 中,存在四个组:
1 ((A)(B(C)))
2 (A)
3 (B(C))
4 (C)
组零始终代表整个表达式
3.组的使用及替换:
public static void main(String[] args) { String str1_1= "andittttbobmmmmmmmcyanxxdenim";//源字符串 String regex1= "(.)\\1+"; //目标字符串: andi#bob#cyan#denim String str1_2= str1_1.replaceAll(regex1, "#"); System.out.println("str1_2: "+ str1_2); String str2_1= "andittttbobmmmmmmmcyanxxdenim";//源字符串 String regex2= "(.)\\1+"; //目标字符串: anditbobmcyanxdenim String str2_2= str2_1.replaceAll(regex2, "$1"); System.out.println("str2_2: "+ str2_2); }
output:
str1_2: andi#bob#cyan#denim
str2_2: anditbobmcyanxdenim
总结:在String.replaceAll()方法中,使用$符号来让第二个参数使用第一个参数的值
4.1切割:
public static void main(String[] args) { // TODO Auto-generated method stub String path1= "com/westward/p/5399166";//源字符串 //目标字符串com.westward.5399166 String regex= "/"; String path2= path1.replaceAll(regex, "."); System.out.println(path2); }
output:
com.westward.p.5399166
4.2切割:
public static void main(String[] args) { // TODO Auto-generated method stub String str= "15800001111";//源字符串 // 目标字符串:158****1111; String regex= "(\\d{3})\\d{4}(\\d{4})"; String str2= str.replaceAll(regex, "$1****$2"); System.out.println("str2: "+str2 ); }
output:
str2: 158****1111
5.获取
public static void main(String[] args) { // TODO Auto-generated method stub String str= "hah,cheng wei da shen zhi lu!"; String regex= "\\b\\w{3}\\b"; Pattern pattern= Pattern.compile(regex);//将正则表达式封装为Pattern类的对象 Matcher matcher= pattern.matcher(str);//正则对象匹配字符串,生成Matcher对象 System.out.println(str); while (matcher.find()) {//字词匹配一个,判断有无符合 System.out.println(matcher.group()+";startindex: "+ matcher.start()+ " ,endindex: "+ matcher.end() ); //。group:取出匹配的 .start:匹配的下标(首字母下标,尾字母下标+1), .end()类似.start() } }
正则表达式的一些小练习:
public static void main(String[] args) { System.out.println("sayRight() run:"); sayRight(); System.out.println("sortIp() run:"); sortIp(); System.out.println("checkEmail() run:"); checkEmail(); } public static void sayRight(){ //1.切割 String string= "我我...我我...我我我要...要要要要......要要要要...学学学学学学......学学编编.......编编编编...编..程程....程程"; string= string.replaceAll("\\.+", ""); System.out.println(string); //2.替换 string= string.replaceAll("(.)\\1+", "$1"); System.out.println(string); } //Example:192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55 //对上面的ip进行排序,升序 public static void sortIp(){ String string1= "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55"; //1.为了保持位数一直,首先数字前加两个0 string1= string1.replaceAll("(\\d+)", "00$1"); System.out.println(string1); // 00192.00168.0010.0034 00127.000.000.001 003.003.003.003 00105.0070.0011.0055 //2.替换为位数一致 string1= string1.replaceAll("0*(\\d{3})", "$1");//对于00192,0*表示0出现2次;对于0034,0*表示0出现1次;对于003,0*表示0出现0次; System.out.println(string1); //192.168.010.034 127.000.000.001 003.003.003.003 105.070.011.055 //3.切割,放到字符串数组 String[] strings= string1.split(" +"); TreeSet<String> treeSet= new TreeSet<String>(); for (String string : strings) { treeSet.add(string);//TressSet:默认的为升序 } System.out.println("排序后的: "); for (String string : treeSet) { System.out.println(string); } } public static void checkEmail(){ String string= "whjweo123_2@qq.com.c22"; String regex= "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{2,3}){1,3}"; boolean isEmail= string.matches(regex); System.out.println("isEmail: "+ isEmail); }
output:
sayRight() run:
我我我我我我我要要要要要要要要要学学学学学学学学编编编编编编编程程程程
我要学编程
sortIp() run:
00192.00168.0010.0034 00127.000.000.001 003.003.003.003 00105.0070.0011.0055
192.168.010.034 127.000.000.001 003.003.003.003 105.070.011.055
排序后的:
003.003.003.003
105.070.011.055
127.000.000.001
192.168.010.034
checkEmail() run:
isEmail: false