一个字符串有多个img如何取出来 ?

原址:http://blog.csdn.net/yuan8080/article/details/6899211 

public static Map<String,String> getImgStr(String htmlStr){
        String img="";
        Pattern p_image;
        Matcher m_image;
        Map<String,String> pics = new HashMap<String, String>();

        //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址

        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img,Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        int i=1;
        while(m_image.find()){
            img = img + "," + m_image.group();
            // Matcher m  = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src

            Matcher m  = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);

            while(m.find()){
                pics.put(String.valueOf(i),m.group(1));
                i++;
            }
        }
        return pics;
    }
    public static  void  main(String[] args) throws  Exception{
        String str="%3Cp%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2F%E9%94%81%E5%BA%93%E5%AD%98%E9%80%9A%E7%9F%A5.jpg%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_01.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_02.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_03.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_04.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_05.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_06.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_07.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_08.jpg%3Fv%3D233000%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_09.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_10.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_11.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_12.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_13.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_14.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_15.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_16.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_17.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_18.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_19.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_20.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_21.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_22.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_23.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_24.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_25.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_26.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_27.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_28.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_29.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3C%2Fp%3E";
        Map<String,String> map=    getImgStr(URLDecoder.decode(str,"UTF-8"));
        System.out.println(map.values());
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值