1.應(yīng)用技術(shù)
使用java.util.regex包下的Pattern和Matcher兩個(gè)類
-
Pattern類
- 創(chuàng)建正則表達(dá)式
用于創(chuàng)建一個(gè)正則表達(dá)式,也可以說(shuō)創(chuàng)建一個(gè)匹配模式羔杨,它的構(gòu)造方法是私有的,不可以直接創(chuàng)建,但可以通過(guò)Pattern.complie(String regex)簡(jiǎn)單工廠方法創(chuàng)建一個(gè)正則表達(dá)式抹腿。
Pattern p=Pattern.compile("\\w+");
- 分隔字符串
Pattern有一個(gè)split()方法,用于分隔字符串,并返回一個(gè)String[]。
Pattern p=Pattern.compile("\\d+"); String[] str=p.split("我的QQ是:456456我的電話是:0532214我的郵箱是:aaa@aaa.com"); //結(jié)果:str[0]="我的QQ是:" str[1]="我的電話是:" str[2]="我的郵箱是:aaa@aaa.com"
- 用于快速匹配字符串(Pattern的靜態(tài)方法)
matches是一個(gè)靜態(tài)方法,用于快速匹配字符串,該方法適合用于只匹配一次,且匹配全部字符串兵拢。
Pattern.matches("\\d+","2223");//返回true Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,這里aa不能匹配到 Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,這里bb不能匹配到
- 創(chuàng)建正則表達(dá)式
-
Matcher類
- 創(chuàng)建匹配器
Matcher類的構(gòu)造方法也是私有的,不能隨意創(chuàng)建,只能通過(guò)Pattern.matcher(CharSequence input)方法得到該類的實(shí)例介褥。
Pattern p=Pattern.compile("\\d+"); Matcher m=p.matcher("22bb23"); m.pattern();//返回p 也就是返回該Matcher對(duì)象是由哪個(gè)Pattern對(duì)象的創(chuàng)建的
- Matcher類提供了對(duì)正則表達(dá)式的分組支持,以及對(duì)正則表達(dá)式的多次匹配支持(重點(diǎn))已慢,需要將Pattern與Matcher一起合作曲聂。
public static void main(String[] args) { // TODO Auto-generated method stub String str = "Hello,World! in Java."; Pattern pattern = Pattern.compile("W(or)(ld!)"); Matcher matcher = pattern.matcher(str); while(matcher.find()){ System.out.println("Group 0:"+matcher.group(0));//得到第0組——整個(gè)匹配 System.out.println("Group 1:"+matcher.group(1));//得到第一組匹配——與(or)匹配的 System.out.println("Group 2:"+matcher.group(2));//得到第二組匹配——與(ld!)匹配的,組也就是子表達(dá)式 System.out.println("Start 0:"+matcher.start(0)+" End 0:"+matcher.end(0));//總匹配的索引 System.out.println("Start 1:"+matcher.start(1)+" End 1:"+matcher.end(1));//第一組匹配的索引 System.out.println("Start 2:"+matcher.start(2)+" End 2:"+matcher.end(2));//第二組匹配的索引 System.out.println(str.substring(matcher.start(0),matcher.end(1)));//從總匹配開(kāi)始索引到第1組匹配的結(jié)束索引之間子串——Wor } } 運(yùn)行結(jié)果: Group 0:World! Group 1:or Group 2:ld! Start 0:6 End 0:12 Start 1:7 End 1:9 Start 2:9 End 2:12 Wor
- 創(chuàng)建匹配器
2.應(yīng)用場(chǎng)景
- 返回?zé)o規(guī)則的數(shù)據(jù)佑惠,需要解析,如需要獲取code和uuid的值
//返回?cái)?shù)據(jù)
window.QRLogin.code = 200; window.QRLogin.uuid = "XXXXXXX"
//解決方法
Pattern UUID_PATTERN = Pattern.compile("window.QRLogin.code = (\\d+); window.QRLogin.uuid = \"(\\S+?)\";");
private String getUUID() {
String response = getForObject(URL_UUID);
Matcher matcher = UUID_PATTERN.matcher(response);
if (matcher.find() && StateCode.SUCCESS.equals(matcher.group(1))) {
return matcher.group(2);
}
return "";
}
- 返回xml的數(shù)據(jù)朋腋,需要解析skey,wxsid,pass_ticket等值
<error>
<ret>0</ret>
<message>OK</message>
<skey>xxx</skey>
<wxsid>xxx</wxsid>
<wxuin>xxx</wxuin>
<pass_ticket>xxx</pass_ticket>
<isgrayscale>1</isgrayscale>
</error>
解決方法:
public static String match(String reg, String text) {
Pattern pattern = Pattern.compile(reg);
Matcher m = pattern.matcher(text);
if (m.find()) {
return m.group(1);
}
return null;
}
loginSession.setSKey(match("<skey>(\\S+)</skey>", body));
loginSession.setWxSid(match("<wxsid>(\\S+)</wxsid>", body));
loginSession.setWxUin(match("<wxuin>(\\S+)</wxuin>", body));
loginSession.setPassTicket(match("<pass_ticket>(\\S+)</pass_ticket>", body));
解析json格式數(shù)據(jù):
{
"code":"1",
"message":"ok",
"data":{
"totalCount":2,
"pageCount":1,
"rowsCount":2,
"data":{
"311715616060001932":{
"BarCode":"8809016360267",
"GoodsNo":"0",
"HgGoodsNo":"311715616060001932",
"SyQty":0,
"Price":20.23,
"GyQty":0,
"PracticalQty":0,
"GoodsBasisNo":"009603080029"
}
}
}
}
//解析
GoodsStockDto goodsStockDto = new GoodsStockDto();
goodsStockDto.setGoodsSn(match("\"HgGoodsNo\":\"(\\w+)\"", responseContent));
goodsStockDto.setGoodsQty(match("\"SyQty\":([0-9.]+)", responseContent));
String rtnCode = match("\"code\":\"(\\d+)\"", responseContent);
if (!SUCCESS_CODE.equals(rtnCode))
{
dto.setRtnCode(ThirdConstants.FAILED);
}
//字母加空白符
dto.setRtnMsg(match("\"message\":\"([A-Za-z\\s]+)\"", responseContent));
dto.setResult(goodsStockDto);