通过正则表达式提取Markdown文本中的图片路径

public static Set<String> getMdImgUrl(String mdStr){
     Set<String> imgSet = new HashSet<>();
     Pattern pattern = Pattern.compile("!\\[(.*?)\\]\\((.*?)\\)",Pattern.CASE_INSENSITIVE);
     Matcher matcher = pattern.matcher(mdStr);
     while(matcher.find()){
         String imgUrl = matcher.group(2);
         imgSet.add(imgUrl);
     }
     return imgSet;
 }

 public static void main(String[] args) {
     String mdStr = FileUtil.readString(new File("C:\\Users\\YL\\Desktop\\临时文件\\新建文本文档.md"), "utf-8");
     Set<String> mdImgUrl = getMdImgUrl(mdStr);
     System.out.println(JSON.toJSONString(mdImgUrl));
 }

你可能感兴趣的:(小功能实现,正则表达式,java,开发语言)