黑马程序员_学习笔记第24天——正则表达式

----------------------  ASP.Net+Android+IOS开发 href="http://edu.csdn.net"target="blank">.Net培训 、期待与您交流! ----------------------

1、正则表达式:符合一定规则的表达式

作用:用于专门操作字符串

特点:用于一些特定的符号来表示一些代码操作,这样就简化书写。

好处:可以简化对字符串的复杂操作。

弊端:符号定义越多,正则越长,阅读性越差

2、具体操作功能:

匹配:String  matches方法,用规则匹配整个字符串,只要有一处不符合规则,就匹配结束,返回false

切割:String  split()

替换:String  replaceAll()

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class RegexDemo1 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         //checkQQ();  
  5.         //checkTel();  
  6.         //splitDemo("zhangsan.lisi.wangwu","\\.");  
  7.         //splitDemo("c:\\abc\\a.txt","\\\\");  
  8.         //splitDemo("askfkkkdakjsqqkf","(.)\\1+");//按照叠词完成切割,为了可以让规则结果被重用  
  9.                                                 //可以将规则封装成一个组,用()完成。组的出现都有编号  
  10.                                                 //从1开始,想要使用已有的组可以通过\n(n就是组的编号)的形式来获取。  
  11.         //String str = "wtwer14125243554sdf341241afjl341d";//将字符串中的数字替换成#  
  12.         //replaceAllDemo(str,"\\d{5,}","#");  
  13.           
  14.         String str1 = "asdffkjkeeafaiiezzzzzle";//将叠词替换成& //将重叠字幕替换成单个字母  
  15.         replaceAllDemo(str1,"(.)\\1+","&");  
  16.         replaceAllDemo(str1,"(.)\\1+","$1");  
  17.           
  18.     }  
  19.     //校验qq号  
  20.     public static void checkQQ(){  
  21.         String qq = "870987324";  
  22.         String regex = "[1-9][0-9]{4,14}";  
  23.         boolean flag = qq.matches(regex);  
  24.         if(flag)  
  25.             System.out.println(qq+"...is ok");  
  26.         else  
  27.             System.out.println(qq+"不合法...");  
  28.     }  
  29.     //校验手机号,手机号段只有13xx,15xx,18xx  
  30.     public static void checkTel(){  
  31.         String tel = "13451075098";  
  32.         String reg = "1[358]\\d{9}";  
  33.         boolean flag = tel.matches(reg);  
  34.         System.out.println(flag);  
  35.     }  
  36.     public static void splitDemo(String str,String reg){  
  37. //      String str = "zhangsan.lisi.wangwu";  
  38. //      String reg = " +";//按照多个空格来进行切割  
  39. //      String reg = "\\.";  
  40.         String[] arr = str.split(reg);  
  41.         System.out.println(arr.length);  
  42.         for(String s : arr){  
  43.             System.out.println(s);  
  44.         }  
  45.     }  
  46.     public static void replaceAllDemo(String str,String reg,String newStr){  
  47.         str = str.replaceAll(reg,newStr);  
  48.         System.out.println(str);  
  49.           
  50.     }  
  51.   
  52. }  

3、获取:将字符串中的符合规则的子串取出

操作步骤:

1)将正则表达式封装成对象

2)让正则对象和要操作的字符串相关联

3)关联后,获取正则匹配引擎

4)通过引擎对符合规则的子串进行操作,比如取出

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class RegexDemo2 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         getDemo();  
  5.   
  6.     }  
  7.     public static void getDemo(){  
  8.         String str = "ming tian jiu yao fang jia le";  
  9.         String reg ="\\b[a-z]{3}\\b";  
  10.         //将规则封装成对象  
  11.         Pattern p = Pattern.compile(reg);  
  12.         //让正则对象和要作用的字符串相关联,获取匹配器对象  
  13.         Matcher m = p.matcher(str);  
  14. //      boolean b = m.find();//将规则作用到字符串上,并进行复核规则的子串查找  
  15. //      System.out.println(b);  
  16. //      System.out.println(m.group());//用于获取匹配后结果  
  17.         while(m.find()){  
  18.             System.out.println(m.group());  
  19.         }  
  20.     }  
  21. }  
4、练习

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //需求:将下列字符串转成:我要学编程  
  2. /* 
  3. 思路: 
  4. 1、如果只想知道该字符串是对是错,使用匹配 
  5. 2、想要将已有的字符串变成另一个字符串,替换 
  6. 3、想要按照自定义 的方式将字符串变成多个字符串,切割,获取规则以外的子串 
  7. 4、想要拿到符合需求的字符串子串,获取,获取符合规则的子串 
  8.  */  
  9. public class RegexTest1 {  
  10.   
  11.     public static void main(String[] args) {  
  12.         String str1 = "我我...我我..我要...要要..要要..学学学...学学...编编编...编程...程程..程..程";  
  13.         String str2 = str1.replaceAll("(\\.)\\1+","");  
  14.         System.out.println(str2);  
  15.         String str3 = str2.replaceAll("(.)\\1+","$1");  
  16.         System.out.println(str3);  
  17.     }  
  18. }  
5、ip地址段排序

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2. 192.168.1.254  102.49.23.13  10.10.10.10  2.2.2.2  8.109.90.30 
  3. 将ip地址进行地址段顺序的排序 
  4. 还按照字符串自然顺序,只要让它们每一段都是3位即可 
  5. 1、按照每一段需要的最多的0进行补齐,那么每一段就会至少保证有3位 
  6. 2、将每一段只保留3三位,这样,所有的ip地址都是每一段3位 
  7.  */  
  8. public class RegexTest2 {  
  9.   
  10.     public static void main(String[] args) {  
  11.         ipSort();  
  12.   
  13.     }  
  14.     public static void ipSort(){  
  15.         String ip ="192.168.1.254  102.49.23.13  10.10.10.10  2.2.2.2  8.109.90.30";  
  16.         ip = ip.replaceAll("(\\d+)","00$1");  
  17.         System.out.println(ip);  
  18.         ip = ip.replaceAll("0*(\\d{3})","$1");  
  19.         System.out.println(ip);  
  20.         String[] arr = ip.split(" +");  
  21.         TreeSet<String> ts = new TreeSet<String>();  
  22.         for(String s : arr){  
  23.             ts.add(s);  
  24.         }  
  25.         for(String s : ts){  
  26.             System.out.println(s.replaceAll("0*(\\d+)","$1"));  
  27.         }  
  28.         /*Arrays.sort(arr); 
  29.         for(String s : arr){ 
  30.             System.out.println(s); 
  31.         }*/  
  32.     }  
  33.   
  34. }  
6、校验邮箱地址

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //需求:对邮件地址进行校验  
  2. public class RegexTest3 {  
  3.   
  4.     public static void main(String[] args) {  
  5.         checkMail();  
  6.     }  
  7.     public static void checkMail(){  
  8.         String mail = "abc12@sina.com.cn";  
  9.         String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";//较为精确的匹配  
  10. //      reg = "\\w+@\\w+(\\.\\w)+";//相对不太精确的匹配  
  11.         System.out.println(mail.matches(reg));  
  12.     }  
  13. }  
7、网页爬虫

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //网页爬虫(蜘蛛)  
  2. public class RegexTest4 {  
  3.   
  4.     public static void main(String[] args) throws Exception{  
  5.         //getMails1();  
  6.         getMails2();  
  7.     }  
  8.     //获取指定文档中的邮件地址,使用获取功能,Pattern Matcher  
  9.     public static void getMails1() throws Exception {  
  10.         BufferedReader bufr = new BufferedReader(new FileReader("d:\\mail.txt"));  
  11.         String line = null;  
  12.         String mailreg = "\\w+@\\w+(\\.\\w+)+";  
  13.         Pattern p = Pattern.compile(mailreg);  
  14.         while((line=bufr.readLine())!=null) {  
  15.             Matcher m = p.matcher(line);  
  16.             while(m.find()){  
  17.                 System.out.println(m.group());  
  18.             }  
  19.         }  
  20.     }  
  21.     public static void getMails2() throws Exception {  
  22.         URL url = new URL("http://192.168.1.102:8080/myweb/mail.html");  
  23.         URLConnection conn = url.openConnection();  
  24.         BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  25.         String line = null;  
  26.         String mailreg = "\\w+@\\w+(\\.\\w+)+";  
  27.         Pattern p = Pattern.compile(mailreg);  
  28.         while((line=bufIn.readLine())!=null) {  
  29.             Matcher m = p.matcher(line);  
  30.             while(m.find()){  
  31.                 System.out.println(m.group());  
  32.             }  
  33.         }  
  34.     }  
  35.   
  36. }  





----------------------  ASP.Net+Android+IOS开发 href="http://edu.csdn.net"target="blank">.Net培训 、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值