java通过正则表达式获取HTML指定的属性值

 

方法如下:

//从HTML标签获取图片宽高
    //contentHtml: html标签
    //string : 标签属性名
    private Integer getImgWidthOrHeight(String contentHtml,String string){
        String widthReg = "<" + "img" + "[^<>]*?\\s" + string + ":['\"]?(.*?)['\"]?\\s.*?>";
        Matcher widthRegM = Pattern.compile(widthReg).matcher(contentHtml);
        String widthStr = "";
        while (widthRegM.find()) {
            widthStr = widthRegM.group(1).replace("px", "");
            widthStr = widthStr.replace(";", "");
        }
        return Integer.valueOf(widthStr);
    }

 

posted @ 2021-10-21 17:01  小小穿梭机^^  阅读(383)  评论(0编辑  收藏  举报