Extract image src from

后端 未结 2 1318
萌比男神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 = "whatever"
    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.

提交回复
热议问题