黑马程序员 Java自学总结十七 正则表达式

------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------

总结内容来源于黑马毕老师的java基础教学视频


正则表达式:符合一定规则的表达式.
    学习正则表达式,就是学习一些特殊符号的应用.
    作用:用于专门操作字符串.
    特点:用一些特点的符号来表示一些代码操作.简化书写.

    好处:可以简化对字符串的复杂操作.
    弊端:符号定义越多,正则越长,阅读性越差.

各种符号的意义请参见API文档

具体操作功能:

1.匹配:String matches方法,用规则匹配整个字符串,返回boolean型

2.切割:String split方法

3.替换:String replaceAll方法

[html]  view plain copy
  1. class  RegexDemo    
  2. {    
  3.     public static void main(String[] args)     
  4.     {    
  5.         matchDemo("2334498398239","1[358]\\d{9}");    
  6.         matchDemo("13842229504","1[358]\\d{9}");    
  7.         System.out.println("------------匹配----------------");    
  8.     
  9.         splitDemo("andy.lili.tony","\\.");//按"."切,注意要用\\.     
  10.         splitDemo("kdsjfksf  kdsfjksdf    dkffj kdfj jf"," +");//按 空格切    
  11.         splitDemo("g:\\java\\code\\day25","\\\\");//按”//“切    
  12.         splitDemo("dfkdddjfkdaadfzzzzkkkq","(.)\\1+");//按叠词切    
  13.         System.out.println("------------切割----------------");    
  14.             
  15.         replaceDemo("aadddeeeffdoe","(.)\\1+","$1");//将重叠的字符替换成单个字母。iiii>>i 用 $ 获取前一个正则表达式的组。    
  16.         replaceDemo("andy12;number1388882228384;","\\d{5,}","*");//将字符串中的数字替换成*。    
  17.         replaceDemo("cdddffeeaadfafkkjkqqccg","(.)\\1+","#");//将叠词提出成#    
  18.     }    
  19.     public static void matchDemo(String s,String reg)    
  20.     {    
  21.         boolean flag = s.matches(reg);    
  22.         if(flag)    
  23.             System.out.println("手机号输入正确");    
  24.         else    
  25.             System.out.println("输入号码有误");    
  26.     }    
  27.     public static void splitDemo(String s,String reg)    
  28.     {    
  29.         String[] arr = s.split(reg);    
  30.         System.out.println(arr.length);    
  31.         for(String str:arr)    
  32.         {    
  33.             System.out.println(str);    
  34.         }    
  35.     }    
  36.     public static void replaceDemo(String s,String reg,String rp)    
  37.     {    
  38.         String ars.replaceAll(reg,rp);    
  39.         System.out.println(ar);    
  40.     }    
  41. }    

4.用于复杂的获取,要与pattern和matcher配合使用;

操作步骤:
1)导入正则包,import java.regex.*;
2)将正则表达式封装成pattern对象;
3)将正则对象和要操作的字符串关联;
4)关联后,获取正则匹配引擎,matcher
5)通过引擎,对符合规则的子串进行操作;
代码:
[html]  view plain copy
  1. import java.util.regex.*;//1    
  2. class GetDemo     
  3. {    
  4.     public static void main(String[] args)     
  5.     {    
  6.         String s = "adfddfdfflkljkhjjj;; woifieujjfjfslfjdfad;w";    
  7.     
  8.         String reg="(.)\\1+";    
  9.     
  10.         Pattern p = Pattern.compile(reg);//2    
  11.     
  12.         Matcher m = p.matcher(s);//3    
  13.     
  14.         while(m.find())//4    
  15.         {    
  16.             System.out.println(m.group());    
  17.         }    
  18.     }    
  19. }    

如何判断要选取那种功能进行操作?
1.如果只想知道该字符串是对是错,使用匹配;
2.想要将自己的字符串变成另一个字符串,用替换;
3.想要按照指定的方式将字符串变成多个字符串,用切割;
4.想要拿到符合需求的子串,使用获取;
练习:
[html]  view plain copy
  1. import java.util.*;    
  2. class  Test    
  3. {    
  4.     public static void main(String[] args)     
  5.     {    
  6.         test1();     
  7.         ipSort();    
  8.         checkMail();    
  9.     }    
  10.     public static void test1()    
  11.     {    
  12.         String str = "黑黑.....黑黑黑....马程...程程....程.....程序...序..序序序.....员...员";     
  13.          //str=str.replaceAll(str,"黑马程序员");    
  14.          str=str.replaceAll("\\.","");    
  15.          str=str.replaceAll("(.)\\1+","$1");    
  16.          System.out.println(str);    
  17.     }    
  18.     /*   
  19.      需求2: 将下边的ip地址段进行地址段顺序的排序。    
  20.      192.168.1.15    23.48.56.109    10.73.91.18    254.253.252.1    1.23.25.26     
  21.     
  22.      思路:    
  23.      还按照字符自然顺序,只要让它们每一段都是3位即可,    
  24.      1.按照每一段需要的最多的0进行补齐,那么每一段就会至少保证有3位。    
  25.      2.将每一段只保留三位,这样所有的ip地址都是每一段3位。    
  26.      */    
  27.       public static void ipSort()      
  28.      {      
  29.         String ip = "92.168.1.15 23.48.56.109 10.73.91.18 254.253.252.1 1.23.25.26";      
  30.         ip = ip.replaceAll("(\\d+)","00$1");      
  31.         System.out.println(ip);      
  32.         ip = ip.replaceAll("0+(\\d{3})","$1");      
  33.         String [] arr = ip.split(" +");      
  34.         //Arrays.sort(arr);//如果有重复的IP地址并要获取,那么必须用这个      
  35.         TreeSet<String> ts = new TreeSet<String>();           
  36.         for(String str:arr)      
  37.         {         
  38.             //System.out.println(str);                
  39.             ts.add(str);                  
  40.         }      
  41.         for(String s:ts)      
  42.         {      
  43.             System.out.println(s.replaceAll("0+(\\d+)","$1"));      
  44.         }      
  45.               
  46.         /*    
  47.         Iterator<String> it = ts.iterator();    
  48.         while(it.hasNext())    
  49.         {    
  50.             String str = it.next();    
  51.             str=str.replaceAll("0+(\\d+)","$1");    
  52.             System.out.println(str);    
  53.         }    
  54.         */      
  55.      }    
  56.      /*    
  57.     需求3:对邮件地址进行校验。这个必须掌握    
  58.     */      
  59.     public  static void checkMail()      
  60.     {      
  61.         String mail ="asdfas12@sina.com.cn";      
  62.         //mail = "1@1.1";      
  63.         String reg = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+){1,3}";      
  64.         //reg = "\\w+@\\w+(\\.\\w+)";//相对不太精确的匹配。      
  65.         //mail.indexOf("@")!=-1;这种方式不要用。      
  66.         System.out.println(mail.matches(reg));      
  67.     }         
  68.     
  69. }    

网络爬虫(蜘蛛):
是搜索引擎的原理之一,用以检索网上的信息,如邮箱地址,博客,关键字等等;
[html]  view plain copy
  1. import java.io.*;    
  2. import java.util.regex.*;    
  3. import java.net.*;    
  4. class  RegexTest2    
  5. {    
  6.     public static void main(String[] args) throws IOException    
  7.     {    
  8.         getMails_2();           
  9.     }    
  10.     //需求2:从网页上获取邮件地址    
  11.     public static void getMails_2() throws IOException    
  12.     {    
  13.         //建立应用层用于连接的端点URLConnection    
  14.         URL url = new URL("http://www.163.com/");    
  15.         URLConnection conn = url.openConnection();    
  16.         //定义读取流,用于读取服务器返回的数据    
  17.         BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));    
  18.         String line = null;         
  19.         //定义正则表达式,并获取Pattern对象。    
  20.         String reg = "\\w+@\\w+(\\.\\w+)+";    
  21.         Pattern p = Pattern.compile(reg);    
  22.         while((line = bufIn.readLine())!=null)    
  23.         {       
  24.             //获取与某一字符串的关联的正则表达式配适器。    
  25.             Matcher m = p.matcher(line);    
  26.             while(m.find())    
  27.             {    
  28.                 System.out.println(m.group());    
  29.             }               
  30.         }    
  31.     }    
  32.     /*   
  33.     需求1:获取指定文档中的邮件地址。   
  34.     使用获取功能。Pattern Matcher   
  35.     */    
  36.     public static void getMails() throws IOException    
  37.     {    
  38.         //http://localhost:8080/myweb/mail.html         
  39.         BufferedReader bufr = new BufferedReader(new FileReader("mail.txt"));    
  40.         String line = null;         
  41.         String reg = "\\w+@\\w+(\\.\\w+)+";    
  42.         Pattern p = Pattern.compile(reg);    
  43.         while((line = bufr.readLine())!=null)    
  44.         {               
  45.             Matcher m = p.matcher(line);    
  46.             while(m.find())    
  47.             {    
  48.                 System.out.println(m.group());    
  49.             }    
  50.                 
  51.         }    
  52.     }    
  53. }    



------ ASP.Net+Android+IO开发.Net培训期待与您交流! ------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值