[英]Extract image src from <img>tag in android
我從服務器有這個img標簽。 任何人都可以告訴代碼從下面的標簽中提取圖像鏈接。
> <img id="prescription_image"
> onClick=showPrescriptionDetails("34173cb38f07f89ddbebc2ac9128303f")
> onmouseover="this.style.cursor='pointer'"
> src="http://patient/1409210919.png" width="20" height="20">
非常感謝。
有點像:
int start = tags.indexOf("src=\"") + 5;
int end = tags.indexOf("\"", start);
String src = tags.substring(start, end);
使用正則表達式的更簡單的證明:
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);
}
這將解決上一個解決方案可能存在的大/小寫問題,並嘗試處理更多不尋常的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.