从<img/>标签android中提取图像src

时间:2016-06-16 19:56:38

标签: android html regex

我在html中有一些img标签。我使用正则表达式来获取img标签。我写了一些代码,而我的代码只处理了一个img标记。

String imgRegex = "<[iI][mM][gG][^>]+[sS][rR][cC]\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";

        Pattern p = Pattern.compile(imgRegex);
        Matcher m = p.matcher(htmlString);

        if (m.find()) {
            String imgSrc = m.group(1);

            String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/tanadgomaaa";
            String imagePath = "file://"+ base + "/test.jpg";
            imgSrc=imgSrc.replace(imgSrc,imagePath);



        }

可以告诉我如何从html字符串中获取所有img标签吗? 感谢

1 个答案:

答案 0 :(得分:1)

    String imgRegex = "(?i)<img[^>]+?src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";

    Pattern p = Pattern.compile(imgRegex);
    Matcher m = p.matcher(htmlString);

    while(m.find()) {
        String imgSrc = m.group(1);

        String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/tanadgomaaa";
        String imagePath = "file://"+ base + "/test.jpg";
        imgSrc=imgSrc.replace(imgSrc,imagePath);



    }