<dt><h3>证书</h3></dt><dd>2009年9月:交通部公路工程专业监理工程师<br />证书描述:jgz0938335<br />2004年10月:水利部总监理工程师<br />证书描述:证书编号jlz2005080248<br />2004年6月:国家注册监理工程师<br />证书描述:注册号42001265<br />2003年12月:水利部监理工程师<br />证书描述:证书编号jlg2005080919<br />
成:
ymStr=2009年9月:yyyy-MM=2009-9,licType=交通部公路工程专业监理工程师,licTypeDesc=jgz0938335
ymStr=2004年10月:yyyy-MM=2004-10,licType=水利部总监理工程师,licTypeDesc=证书编号jlz2005080248
ymStr=2004年6月:yyyy-MM=2004-6,licType=国家注册监理工程师,licTypeDesc=注册号42001265
ymStr=2003年12月:yyyy-MM=2003-12,licType=水利部监理工程师,licTypeDesc=证书编号jlg2005080919
开始一直想去用正则扣,最后选择向前截取,而不是向后
最终的写法:
int licTypeInfoSInx = sb2.indexOf("<dt><h3>证书</h3></dt><dd>");
List<JSONObject> licTypeList = new ArrayList<JSONObject>();
if(licTypeInfoSInx!=-1){
int licTypeInfoEInx = sb2.indexOf("</dd></dl>",licTypeInfoSInx);
licTypeInfo = sb2.substring(licTypeInfoSInx, licTypeInfoEInx);
System.out.println("licTypeInfo:"+licTypeInfo);
Pattern p= Pattern.compile("(\\d+)年(\\d+)月:");
Matcher m = p.matcher(licTypeInfo);
StringBuffer appenSb=null;
StringBuffer tailSb=null;
while(m.find()){
// licType,licDate,licPC,presentStatus,title
JSONObject o = new JSONObject();
String ymStr = m.group(),year = m.group(1),month=m.group(2);
String licType="",licTypeDesc="";
appenSb = new StringBuffer();
tailSb = new StringBuffer();
m.appendReplacement(appenSb,ymStr);
m.appendTail(tailSb);
Matcher tempM = p.matcher(tailSb);
if(tempM.find()){
licType = tailSb.substring(0,tempM.start(0));
}else{
licType = tailSb.toString();
}
licType = licType.replaceAll("\\<br\\s/\\>","");
int licTypeDescInx = licType.indexOf("证书描述:");
if(licTypeDescInx!=-1){
licTypeDesc = licType.substring(licTypeDescInx).replaceAll("证书描述:", "");
licType = licType.substring(0, licTypeDescInx);
}
System.out.println("ymStr="+ymStr+"yyyy-MM="+(year+"-"+month)+",licType="+licType+",licTypeDesc="+licTypeDesc);
o.put("licType", licType);
o.put("licTypeDesc", licTypeDesc);
o.put("licDate", year+"-"+month);
licTypeList.add(o);
// inx_s = licTypeInfo.indexOf(ymStr,inx_s);
// inx_e = licTypeInfo.indexOf("<br />", inx_s);
// if(inx_e!=-1){
// licType = licTypeInfo.substring(inx_s, inx_e).replaceFirst(ymStr, "");
// }
// inx_s =licTypeInfo.indexOf("证书描述:");
// if(inx_s!=-1){
// inx_e = licTypeInfo.indexOf("<br />", inx_s);
// licTypeDesc = licTypeInfo.substring(inx_s, inx_e).replaceFirst("证书描述:", "");
// }
}
}