读取md内容中的图片地址
因为发布文章的内容图片上传到minio上, 为了减少空间占用, 所以需要删除文章的同时将内容中的图片也删除掉.
/**
* 将markdown中的图片地址取出
* @param markdown markdown内容
*/
public static List<String> listPicUrlByMarkdown(String markdown) {
List<String> matchList = getMatchList(markdown, "\\(.*\\)");
String stringList = matchList.toString().replaceAll("\\(", "").replaceAll("\\[", "").replaceAll("]", "").replaceAll("\\)", "");
String[] str = stringList.split(",");
return Arrays.asList(str);
}
public static List<String> getMatchList(String markdown, String reg) {
ArrayList<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(markdown);// 指定要匹配的字符串
while (matcher.find()) { //此处find()每次被调用后,会偏移到下一个匹配
result.add(matcher.group());//获取当前匹配的值
}
result.trimToSize();
return result;
}
因为正则表达式不太熟悉, 所以写的不太好, 暂时能用, 之后再改进.
参考链接