Extract image src from

后端 未结 2 1320
萌比男神i
萌比男神i 2020-12-12 02:45

i have this img tag from server. can anyone tell code to extract image link from the below tag.

>  onClick=showPresc         


        
相关标签:
2条回答
  • 2020-12-12 02:57

    More fool proof using regex:

    String html = "<img SRC=\"whatever\">whatever</img>"
    String imgRegex = "<[iI][mM][gG][^>]+[sS][rR][cC]\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
    
    Pattern p = Pattern.compile(imgRegex);
    Matcher m = p.matcher(html);
    
    if (m.find()) {
        String imgSrc = m.group(1);
    }
    

    This will take care of possible upper/lower case issues, which the previous solution has, and also tries to handle more unusual cases.

    0 讨论(0)
  • 2020-12-12 03:13

    Something along the lines of:

    int start = tags.indexOf("src=\"") + 5;
    int end = tags.indexOf("\"", start);
    
    String src = tags.substring(start, end);
    
    0 讨论(0)
提交回复
热议问题