天天看點

徹底搞清楚java正則的兩個類Pattern和Matcher

1.直接上代碼

@Test
    void contextLoads() {
        String regex = "[0-9][0-9]";//兩位數字
        String str = "11我們是22我們是33";
     //1.Pattern類作用
        //1.1調用靜态方法matches判斷正則跟目标對象是否比對
        boolean m = Pattern.matches(regex, str);//false
        //1.2初始化正則模闆,得到模闆對象可以進行多種操作
        Pattern pattern = Pattern.compile(regex);
     //2.模闆對象作用
        //2.1用正則模闆來分離目标
        String[] split = pattern.split(str);//["","我們是","我們是"]
        //2.2用正則模闆來比對目标,得到比對對象
        Matcher matcher = pattern.matcher(str);
     //*3.比對對象的作用
        int i = 2;//三種比對方案
        switch (i) {
            case 1://matcher.matches();将str看成一個整體用regex配置
                boolean matches1 = matcher.matches();//false,跟1.1作用效果一樣
                break;
            case 2://matcher.find();在str中檢視所有符合regex的
                /**
                 * 每次find後matcher都會自動擷取下一個
                 */
                boolean b0 = matcher.find(); //true
                int start = matcher.start();//0
                int end = matcher.end();//2
                String group0 = matcher.group();//11

                boolean b1 = matcher.find(); //true
                int start1 = matcher.start();//5
                int end1 = matcher.end();//7
                String group1 = matcher.group();//22

                boolean b2 = matcher.find(); //true
                int start2 = matcher.start();//10
                int end2 = matcher.end();//12
                String group2= matcher.group();//33
                /**
                 * 等效于下面代碼
                 */
                while (matcher.find()){
                    String group = matcher.group();//11 22 33
                }
                /**
                 * 注意 find(int group)  find(0)從第一個開始周遊   find(n)從第二個開始周遊
                 */
                boolean b3 = matcher.find(1);
                String group3 = matcher.group();//22
                boolean b4 = matcher.find(2);
                String group4 = matcher.group();//22
                boolean b5 = matcher.find(3);
                String group5 = matcher.group();//22
                boolean b = matcher.find(0);
                String group = matcher.group();//11
				 /**
                 * 注意replaceAll()之後不能繼續比對
                 */
                boolean b7 = matcher.find(); //true
                int start7 = matcher.start();//0
                int end7 = matcher.end();//2
                String group07 = matcher.group();//11

                String s = matcher.replaceAll("");//我們是我們是
                boolean b8 = matcher.find(); //false

                break;
            case 3://matcher.lookingAt();在str中從第一位開始比對regex的
                Boolean aBoolean = matcher.lookingAt();//true
                String group6 = matcher.group();//11
                Boolean aBoolean2 = matcher.lookingAt();//true
                String group7 = matcher.group();//11
                Boolean aBoolean4 = matcher.lookingAt();//true
                String group8 = matcher.group();//11
                break;
        }


    }
           

2.總結

正則:regex         目标字元串:str
1.如果判斷regex是否跟str比對,直接用:boolean m = Pattern.matches(regex, str);或boolean matches = str.matches(regex);
2.如果要去除或者替換str中多個跟regex配置的字元串:str.replaceAll(regex,"")或者String s1 = Pattern.compile(regex).matcher().replaceAll(str);
3.如果要操作str中多個跟regex配置的字元串:Matcher matcher= Pattern.compile(regex).matcher(str);然後while循環matcher對象。
           

繼續閱讀