zoukankan      html  css  js  c++  java
  • 正则表达式获取多个img src的值

    /**
    * 得到网页中图片的地址
    */
    public static Set<String> getImgStr(String htmlStr) {
    Set<String> pics = new HashSet<>();
    String img = "";
    Pattern p_image;
    Matcher m_image;
    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()) {
    String tempImgPath=m.group(1);
    pics.add(tempImgPath);
            }
    }
    return pics;
    }

    /**
    *修改入库的图片地址为正式地址
    */
    public static String repairContent(String content){
    String patternStr="<img\s*([^>]*)\s*src=\"(.*?)\"\s*([^>]*)>";
    Pattern pattern = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(content);
    String result = content;
    while(matcher.find()) {
    String src = matcher.group(2);
    String replaceSrc = "download";
    result = result.replaceAll("fileupload-temp",replaceSrc);
    }
    return result;
    }
     
  • 相关阅读:
    函数
    文件处理及处理模式
    字符编码
    元组,字典和集合的用法
    数字类型、字符串和列表
    计算机硬件介绍
    数据类型及语法介绍
    初识python
    设计模式
    最近的时候
  • 原文地址:https://www.cnblogs.com/cecWork/p/9173542.html
Copyright © 2011-2022 走看看