您的位置:首页 > 编程语言 > Java开发

java如何用正则解析HTML中img标签里图片的路径

2016-03-15 17:56 519 查看
content为需要解析HTML的源码,注意是string类型的

private String[] getImgs(String content) {

String img = "";
Pattern p_image;
Matcher m_image;
String str = "";
String[] images = null;
String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(content);
while (m_image.find()) {
img = m_image.group();
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)")
.matcher(img);
while (m.find()) {
String tempSelected = m.group(1);
if ("".equals(str)) {
str = tempSelected;
} else {
String temp = tempSelected;
str = str + "," + temp;
}
}
}
if (!"".equals(str)) {
images = str.split(",");
}
return images;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java html 正则