黑马程序员-学习日记(正则表达式)

 ------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

 

正则表达式的常用构造:

字符类 
    [abc] a、b 或 c(简单类) 
    [^abc] 任何字符,除了 a、b 或 c(否定) 
    [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围) 

    预定义字符类 
    . 任何字符(与行结束符可能匹配也可能不匹配) 
    \d 数字:[0-9] 
    \D 非数字: [^0-9] 
    \s 空白字符:[ \t\n\x0B\f\r] 
    \S 非空白字符:[^\s] 
    \w 单词字符:[a-zA-Z_0-9] 
    \W 非单词字符:[^\w] 

    Greedy 数量词 
    X?       X,一次或一次也没有 
    X*       X,零次或多次 
    X+      X,一次或多次 
    X{n}     X,恰好 n 次 
    X{n,}    X,至少 n 次 
    X{n,m} X,至少 n 次,但是不超过 m 次 


    边界匹配器 
    ^ 行的开头 
    $ 行的结尾 
    \b 单词边界 
    \B 非单词边界 
    \A 输入的开头 
    \G 上一个匹配的结尾 
    \Z 输入的结尾,仅用于最后的结束符(如果有的话) 
    \z 输入的结尾

 

public static void main(String[] args) {
        // TODO Auto-generated method stub
//        functionDemo_1();
        String str = "aoob";
        String reg = "ao?b";//一次或一次也没有
        boolean b = str.matches(reg);
        System.out.println(str+":"+b);   //false
        
        reg = "ao+b";//一次或多次
        b = str.matches(reg);
        System.out.println(b);           //true
        
        str = "ab";
        reg = "ao*b";//零次或多次
        b = str.matches(reg);
        System.out.println(b);          //true
        
        str = "aooooooob";
        reg = "ao{3,}b";//至少3次
        System.out.println(str.matches(reg)); //true
        System.out.println(str.matches("ao{4,6}"));//至少4次,但不超过6次 false
    }

匹配、切割、替换、获取

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

 1 package reg;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class RegexDemo {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         functionDemo_1();
11         System.out.println("------------------------");
12         functionDemo_2();
13         System.out.println("------------------------");
14         functionDemo_3();
15         System.out.println("------------------------");
16         functionDemo_4();
17         System.out.println("------------------------");
18         functionDemo_5();
19         System.out.println("------------------------");
20         functionDemo_6();
21         System.out.println("------------------------");
22         functionDemo_7();
23      
24     }
25     
26     /*
27      *  匹配手机号
28      */
29     public static void functionDemo_1()
30     {
31         String tel = "18918811932";
32         String regex = "1[389]\\d{9}"; //预定义字符类型+数量词
33         boolean b = tel.matches(regex);
34         System.out.println(tel+":"+b);
35     }
36     /*
37      * 切割
38      */
39     public static void functionDemo_2()
40     {
41         String str = "zhang  wang  li  zhao";
42         String[] names = str.split(" +");
43         for(String name: names)
44         {
45             System.out.println(name);
46         }
47     }
48     
49     public static void functionDemo_3()
50     {
51         String str = "zhang.wang.li.zhao";
52         String[] names = str.split("\\.");
53         for(String name:names)
54         {
55             System.out.println(name);
56         }
57     }
58     
59     public static void functionDemo_4(){
60          String str = "zhangsanttttxiaoqiangmmmmmzhaoliu";
61 
62          //(.)表示一组,\\1+表示与第1组相同的出现1次以上
63          String[] names = str.split("(.)\\1+");
64 
65          for(String name : names){
66                  System.out.println(name);
67          }
68     }
69     public static void functionDemo_5()
70     {    
71         String str = "zhangsanttttxiaoqiangmmmmmzhaoliu";
72         //$表示前一个参数的第一组,也就是用捕捉到的字符进行替换
73         str = str.replaceAll("(.)\\1+","$1");
74 
75         System.out.println(str);
76     }
77     public static void functionDemo_6(){
78         String str = "15800001111";
79 
80         str = str.replaceAll("(\\d{3})(\\d{4})(\\d{4})","$1****$2");
81 
82         System.out.println(str);
83     }
84     
85     public static void functionDemo_7(){
86         String str = "you can do better";
87         String regex = "\\b[a-z]{3}\\b";
88         Pattern p = Pattern.compile(regex);
89         Matcher m = p.matcher(str);
90         
91         while (m.find())
92         {
93              System.out.println(m.group());//获取匹配的子序列
94              System.out.println(m.start() + ":" + m.end());
95         }
96         
97     }
98 }

将正则表达式进行对象的封装:

Pattern类为正则表达式的编译表现形式。指定为字符串的正则表达式必须首先被编译为此类的实例。然后可将得到的模式用于创建Matcher对象。依照正则表达式,该对象可以与任意字符序列匹配。

操作步骤:

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

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

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

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

练习1:治口吃

 1 package reg;
 2 
 3 public class RegexTest {
 4 
 5     public static void main(String[] args) {
 6         // TO Auto-generated method stub
 7         test();
 8     }
 9     public static void test()
10     {
11         String str = "你。。。你你你。。。能能能。。。取取取取。。。得得得。。。胜胜胜胜。。。利利利利";
12         //将所有句号替换为一个没有字符
13         str = str.replaceAll("\\。+" , "");
14         
15         //替换叠词
16         str = str.replaceAll("(.)\\1+", "$1");
17         System.out.println(str);
18     }
19 }

练习2:ip地址排序

 1 package reg;
 2 
 3 import java.util.TreeSet;
 4 
 5 public class RegexTest {
 6 
 7     public static void main(String[] args) {
 8         // TO Auto-generated method stub
 9         test2();
10     }21 
22     public static void test2()
23     {
24         String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55";
25         
26         ip_str = ip_str.replaceAll("(\\d+)","00$1");
27         System.out.println(ip_str);
28         
29         //每一段保留数字3位
30         ip_str = ip_str.replaceAll("0*(\\d{3})", "$1");
31         System.out.println(ip_str);
32         
33         String[] ips = ip_str.split(" +");
34         //将切好后的ip地址串数组存入TreeSet,交由其排序
35         TreeSet<String> ts = new TreeSet<String>();
36         for(String ip:ips){
37             ts.add(ip);
38         }
39         for(String ip:ts)
40         {
41             System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
42         }
43     }
44 }

练习3:从网页中扒电子邮件地址

 1 package reg;
 2 
 3 import java.awt.List;
 4 import java.io.BufferedReader;
 5 import java.io.IOException;
 6 import java.io.InputStreamReader;
 7 import java.net.MalformedURLException;
 8 import java.net.URL;
 9 import java.util.ArrayList;
10 import java.util.concurrent.ArrayBlockingQueue;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13 
14 public class RegPachong {
15 
16     public static void main(String[] args) throws IOException {
17         // TODO Auto-generated method stub
18         getMailAddress();
19 
20     }
21     public static ArrayList<String> getMailAddress() throws IOException
22     {
23         URL url = new URL("http://www.itheima.com/aboutt/1376.html");
24         
25         BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
26     
27         String mail_regex = "\\w+@\\(\\.\\w+)+";
28         ArrayList<String> list = new ArrayList<String>();
29         
30         Pattern p = Pattern.compile(mail_regex);
31         
32         String line = null;
33         
34         while ((line = bufr.readLine())!=null)
35         {
36             Matcher m = p.matcher(line);
37             
38             while (m.find()) {
39                 list.add(m.group());
40             }
41         }
42             return list;
43     }
44 }

 

转载于:https://www.cnblogs.com/tozr/p/4643477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值