获取某个字符串中某一部分的字符内容,通常大家会首先想到使用String.substring方法,但String.substring方法对于某些包含特殊字符和HTML标签的处理不是很完善。例如如下字符串,想要获取sign中的字符,用String.subString方法虽然也能拿到sign中的内容,但有缺陷....
String str="<form id='frmBankID' name='frmBankName' method='post' action='partner=\"2088121205591541\"&seller_id=\"2088121205591541\"&out_trade_no=\"FI104010001030900000007\"\n" +
"&subject=\"?????\"&body=\"???-?? 1.6 MT\"&total_fee=\"0.20\"¬ify_url=\"http://func91fcw.vfinance.cn/server/ALIPAY10401-VS.htm\"&service=\"mobile.securitypay.pay\"&payment_type=\"1\"\n" +
"&_input_charset=\"utf-8\"&it_b_pay=\"30m\"&show_url=\"m.alipay.com\"\n" +
"&sign=\"IaNZM3TOiUnJf%2B5%2FxBanH3PrNEONHUedwLwuICFWAE5lbEZZg%2Bp2zeo6D7O36PzhrRya%2BzyjumDNFEuaHyc79cXngkYeyo4%2FQA6ze0iKtQswVoGEl24%2BOkL%2BF%2FvOEmkapKaG%2FMGFn8cacu4XW39TD0KznUSJo1iF0kEC8dRWQVQ%3D\"\n" +
"&sign_type=\"RSA\"'></form>";
String.substring截取:
int s=str.indexOf("sign=");
int t=str.indexOf("&sign_type");
String sign= str.substring(s+5,t-1);
System.out.println("sign==="+sign);
正则表达式截取:
Pattern p = Pattern.compile("sign\\=\"(.*?)\"");//正则表达式
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println(m.group(1));//m.group(1)不包括这两个字符
}