java通过正则表达式获取HTML指定的属性值
方法如下:
//从HTML标签获取图片宽高 //contentHtml: html标签 //string : 标签属性名 private Integer getImgWidthOrHeight(String contentHtml,String string){ String widthReg = "<" + "img" + "[^<>]*?\\s" + string + ":['\"]?(.*?)['\"]?\\s.*?>"; Matcher widthRegM = Pattern.compile(widthReg).matcher(contentHtml); String widthStr = ""; while (widthRegM.find()) { widthStr = widthRegM.group(1).replace("px", ""); widthStr = widthStr.replace(";", ""); } return Integer.valueOf(widthStr); }