donoachua donoachua - 5 months ago 42
HTML Question

Extract image src from <img>tag android

i have some img tags in html.i used Regex to get img tags.i wrote some code and my code working only one img tag

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);



}


can enyone tell me how i can get all img tags from html string?
thanks

Answer
    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);



    }