For example, the response packet is passed over:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<business id=\"GETKPM\">
<body><returncode>0</returncode><returnmsg>NTAwMTAyMDEwMDAyODk3MjAxOTA2MDYxNzI1NDQwNDg2MzEyMw==</returnmsg><
/body>
</business>
I just want to get this NTAwMTAyMDEwMDAyODk3MjAxOTA2MDYxNzI1NDQwNDg2MzEyMw == content, with one interception string methods to slow, looking for a long time found using regular expressions interception.
On the code, I wrote a method.
getString (s, flag); a first pass over a parameter message (string), the second parameter is the set I tag taken information.
pattern is a compiled regular expression, and is a regular expression Mather adapter.
String s= "<?xml version=\"1.0\" encoding=\"utf-8\"?><business id=\"GETKPM\"><body><returncode>0</returncode><returnmsg>NTAwMTAyMDEwMDAyODk3MjAxOTA2MDYxNzI1NDQwNDg2MzEyMw==</return"
+ "msg></body></business>\r\n" ;
String flag = "returnmsg";
getString(s, flag);
}
public static String getString(String s,String flag){
int length = flag.length();
Pattern pattern = Pattern.compile("<"+flag+">.*</"+flag+">"); //匹配项 用法a.*b 贪婪算法,表示a开头b结尾的字符串。
Matcher matcher = pattern.matcher(s);
String ret;
if (matcher.find()) {
ret=matcher.group(0).substring(length+2,matcher.group(0).length()-(length+3));
}
else {
ret="未找到";
}
return ret;
}