?案例:
?Pattern p = Pattern.compile("a*b"); // 生成Pattern實(shí)例(設(shè)置匹配模式【規(guī)則】)- 靜態(tài)方法
?Matcher m = p.matcher("aaaaab");??? // 生成Match實(shí)例(設(shè)置匹配選手【報(bào)名】)
?boolean b = m.matches();??????????? // 匹配選手按匹配模式進(jìn)行匹配操作【全部】-屬簡(jiǎn)單用法
?等價(jià)于:
?boolean b = Pattern.matches("a*b", "aaaaab"); // 靜態(tài)方法 -屬簡(jiǎn)單用法
?總結(jié):? ?
?Pattern.compile(regex).matcher(input).matches() // 案例的鏈?zhǔn)綄?xiě)法 -屬簡(jiǎn)單用法
?等價(jià)于: ?
?Pattern.matches(regex, input); // 靜態(tài)方法-屬簡(jiǎn)單用法
?
【兩個(gè)層面】:
一、Pattern層 ——> 靠Pattern.compile()方法生成
?p.pattern():獲得匹配模式【規(guī)則】(正則表達(dá)式)的字符串形式
?p.split(input):用p匹配模式分割input字符串,返回字符串?dāng)?shù)組
?
?簡(jiǎn)單用法:Pattern.matches(regex, input); // 靜態(tài)方法
二、Matcher層 ——> 依賴(lài)于Pattern層而存在,靠p.matcher()方法生成
?m.pattern():獲得匹配模式【規(guī)則】(正則表達(dá)式)的字符串形式 ?
?m.matches():對(duì)匹配選手(整個(gè)字符串)進(jìn)行完全匹配[簡(jiǎn)單用法] ——> 返回布爾值
?【若開(kāi)頭部分能匹配成功,指針將移動(dòng)至開(kāi)頭,但不能獲取結(jié)果,匹配失敗則不移動(dòng)索引指針】
?m.lookingAt():始終只對(duì)匹配選手開(kāi)頭部分的字符串進(jìn)行匹配 ——> 返回布爾值
?【若開(kāi)頭部分能匹配成功,指針將移動(dòng)至開(kāi)頭,但能獲取結(jié)果,匹配失敗則不移動(dòng)索引指針】
?m.find():對(duì)字符串按索引指針位置進(jìn)行迭代匹配 ——> 返回布爾值
?【每次匹配成功都會(huì)移動(dòng)索引指針至最近一次匹配成功的開(kāi)頭,能拿到匹配結(jié)果,匹配失敗則不移動(dòng)索引指針】
?m.start():返回最近一次匹配的初始索引
?m.end():返回最近一次匹配的結(jié)束索引
?m.group():返回最近一次匹配的子字符串
?
?? 如果有分組,那么:
?? m.groupCount():返回p匹配模式中的分組總數(shù)
?? m.start(i):返回最近一次匹配的第i個(gè)分組的初始索引
?? m.end(i):返回最近一次匹配的第i個(gè)分組的結(jié)束索引
?? m.group(i):返回最近一次匹配的第i個(gè)分組的子字符串
?
?
舉例如下:
?? ???? System.out.println("加載配置信息");
?? ??? ?System.out.println("通過(guò)加載配置信息加載一個(gè)代理工廠(chǎng)Map:");
?? ??? ?System.out.println("這個(gè)Map存放的是接口Class與對(duì)應(yīng)的代理工廠(chǎng)");
?? ??? ?MyInterface myInterface = new SqlSession().getMapper(MyInterface.class);
?? ??? ?List<Object> list = myInterface.query(new Object());
?? ??? ?System.out.println(list.size());
?? ??? ?Pattern p = Pattern.compile("\\d ");
?? ??? ?System.out.println(p.pattern());
?? ??? ?System.out.println(p.toString());
?? ??? ?System.out.println(p.flags());
?? ??? ?System.out.println(" ");
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee").length);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[0]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[1]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[2]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[3]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[4]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[5]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[6]);
?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[7]);
//?? ??? ?System.out.println(p.split("a3b896cd3265ef123g456hh789987gg456ee")[8]);
?? ??? ?System.out.println("---------------");
?? ??? ?System.out.println(Pattern.matches("\\d ", "2223"));?? // true
?? ??? ?System.out.println(Pattern.matches("\\d ", "2223aa")); // false
?? ??? ?System.out.println(Pattern.matches("\\d ", "22bb23")); // false
?? ??? ?
?? ??? ?Pattern p2 = Pattern.compile("\\d ");
?? ??? ?Matcher m2 = p2.matcher("22bb23aa56ff78hh97gg65kk98pp88");
?? ??? ?System.out.println(m2.pattern());???? // \d
?? ??? ?System.out.println(m2.toString());??? // java.util.regex.Matcher[pattern=\d region=0,6 lastmatch=]
?? ??? ?System.out.println(m2.regionStart()); // 0
?? ??? ?System.out.println(m2.regionEnd());?? // 18
?? ??? ?System.out.println("============= ");
?? ??? ?
?? ??? ?System.out.println(m2.matches());???? // false
//?? ??? ?System.out.println(m2.start());
//?? ??? ?System.out.println(m2.end());
//?? ??? ?System.out.println(m2.group());
?? ??? ?
?? ??? ?System.out.println(m2.find());??????? // true
?? ??? ?System.out.println(m2.start());?????? // 4
?? ??? ?System.out.println(m2.end());???????? // 6
?? ??? ?System.out.println(m2.group());?????? // 23
?? ??? ?
?? ??? ?System.out.println(m2.find());??????? // true
?? ??? ?System.out.println(m2.start());?????? // 8
?? ??? ?System.out.println(m2.end());???????? // 10
?? ??? ?System.out.println(m2.group());?????? // 56
?? ??? ?
?? ??? ?System.out.println(m2.find());??????? // true
?? ??? ?System.out.println(m2.start());?????? // 12
?? ??? ?System.out.println(m2.end());???????? // 14
?? ??? ?System.out.println(m2.group());?????? // 78
?? ??? ?
?? ??? ?System.out.println(m2.matches());???? // false 由于 開(kāi)頭部分匹配,指針將移動(dòng)至開(kāi)頭,但不能獲取結(jié)果
?? ??? ?System.out.println(m2.lookingAt());?? // true 由于 開(kāi)頭部分匹配,指針將移動(dòng)至開(kāi)頭,但能獲取結(jié)果
//?? ??? ?System.out.println(m2.start());?????? // 0
//?? ??? ?System.out.println(m2.end());???????? // 2
//?? ??? ?System.out.println(m2.group());?????? // 22
?? ??? ?
?? ??? ?System.out.println(m2.find());??????? // true 再次移動(dòng)索引指針
?? ??? ?System.out.println(m2.start());?????? // 4
?? ??? ?System.out.println(m2.end());???????? // 6
?? ??? ?System.out.println(m2.group());?????? // 23
?? ??? ?
?? ??? ?System.out.println(m2.find());??????? // true
//?? ??? ?System.out.println(m2.start());????? ?
//?? ??? ?System.out.println(m2.end());??????? ?
//?? ??? ?System.out.println(m2.group());
?? ??? ?
?? ??? ?System.out.println("=============>>>>>");
?? ??? ?Pattern p3 = Pattern.compile("(\\d )([a-z] )");
?? ??? ?Matcher m3 = p3.matcher("56aaba2223bb44");
//?? ??? ?System.out.println(m3.matches());
?? ??? ?System.out.println(m3.find());
?? ??? ?System.out.println(m3.groupCount());
?? ??? ?System.out.println("*******");
?? ??? ?System.out.println(m3.start());
?? ??? ?System.out.println(m3.end());
?? ??? ?System.out.println(m3.group());
?? ??? ?System.out.println(m3.start(1));
?? ??? ?System.out.println(m3.end(1));
?? ??? ?System.out.println(m3.group(1));
?? ??? ?System.out.println(m3.start(2));
?? ??? ?System.out.println(m3.end(2));
?? ??? ?System.out.println(m3.group(2));
?? ??? ?
?? ??? ?System.out.println("http://///////////////");
?? ??? ?Pattern p4=Pattern.compile("\\d ");
?? ??? ?Matcher m4=p4.matcher("我的QQ是:1307787223 我的電話(huà)是:18617375789 我的郵箱是:crackhu@163.com");
?? ??? ?while(m4.find()) {
?? ??? ????? System.out.println(m4.group());
?? ??? ????? System.out.print("start:" m4.start());
?? ??? ????? System.out.println(" end:" m4.end());
?? ??? ?}
聯(lián)系客服