正则表达式获取多个img src的值

/**
* 得到网页中图片的地址
*/
public static Set<String> getImgStr(String htmlStr) {
Set<String> pics = new HashSet<>();
String img = "";
Pattern p_image;
Matcher m_image;
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
p_image = Pattern.compile
(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(htmlStr);
while (m_image.find()) {
// 得到<img />数据
img = m_image.group();
// 匹配<img>中的src数据
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while (m.find()) {
String tempImgPath=m.group(1);
pics.add(tempImgPath);
        }
}
return pics;
}

/**
*修改入库的图片地址为正式地址
*/
public static String repairContent(String content){
String patternStr="<img\\s*([^>]*)\\s*src=\\\"(.*?)\\\"\\s*([^>]*)>";
Pattern pattern = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
String result = content;
while(matcher.find()) {
String src = matcher.group(2);
String replaceSrc = "download";
result = result.replaceAll("fileupload-temp",replaceSrc);
}
return result;
}
 

猜你喜欢

转载自www.cnblogs.com/cecWork/p/9173542.html
今日推荐