java如何用正則解析HTML中img標籤裏圖片的路徑


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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章