/** 获取图片字符串中所有链接 */
public Set<String> getImgStr(String htmlStr) {
Set<String> pics = new HashSet<>();
String img = "";
Pattern p_image;
Matcher m_image;
// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
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()) {
pics.add(m.group(1));
}
}
return pics;
}
调用上面方法会返回一个Set
这段代码是一个Java方法,用于从HTML字符串中提取所有图片(img)标签的src属性链接。它使用正则表达式匹配img标签,并通过遍历找到的匹配项来收集src链接。返回的是一个包含所有图片链接的Set集合。

314

被折叠的 条评论
为什么被折叠?



