String content = "<p><img src="http://xxx.com/image/635.png" alt="bg" style="max-width:100%;"><img src="http://ssss.com/image/d17c.jpg" alt="spe-03" style="max-width: 100%;">ggg<br></p><p><br></p>"
private final static Pattern ATTR_PATTERN = Pattern.compile("<img[^<>]*?\\ssrc=[‘\"]?(.*?)[‘\"]?\\s.*?>",Pattern.CASE_INSENSITIVE);
String imgList = "";String img = "";if(StringUtils.hasText(content)){ Matcher matcher = ATTR_PATTERN.matcher(content); while (matcher.find()) { imgList += matcher.group(1) + ","; } System.out.print(imgList);}
//如果获取到了文章中的图片
if(!imgList.equals("")){ //如果有两张或两张以上,取第一张,否则就直接第一张 if(imgList.contains(",")){ String[] split = imgList.split(","); img=split[0]; }else { img=imgList; } //newMap.put("img",img);}
原文地址:https://www.cnblogs.com/XiaoheBlogs/p/12312746.html
时间: 2024-10-08 05:42:21