我在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
标签吗?
感谢
答案 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);
}