原址:http://blog.csdn.net/yuan8080/article/details/6899211
public static Map<String,String> getImgStr(String htmlStr){
String img="";
Pattern p_image;
Matcher m_image;
Map<String,String> pics = new HashMap<String, String>();
// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
p_image = Pattern.compile
(regEx_img,Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(htmlStr);
int i=1;
while(m_image.find()){
img = img + "," + m_image.group();
// Matcher m = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(img); //匹配src
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while(m.find()){
pics.put(String.valueOf(i),m.group(1));
i++;
}
}
return pics;
}
public static void main(String[] args) throws Exception{
String str="%3Cp%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2F%E9%94%81%E5%BA%93%E5%AD%98%E9%80%9A%E7%9F%A5.jpg%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_01.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_02.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_03.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_04.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_05.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_06.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_07.jpg%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_08.jpg%3Fv%3D233000%22+style%3D%22font-size%3A+12px%3B%22+alt%3D%22%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_09.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_10.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_11.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_12.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_13.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_14.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_15.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_16.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_17.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_18.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_19.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_20.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_21.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_22.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_23.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_24.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_25.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_26.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_27.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_28.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3Cimg+src%3D%22http%3A%2F%2Fwww.haitun.hk%2Fimages%2Fupload%2FImage%2FHEFR002_D_29.jpg%22+alt%3D%22%22+style%3D%22font-size%3A+12px%3B%22+%2F%3E%3C%2Fp%3E";
Map<String,String> map= getImgStr(URLDecoder.decode(str,"UTF-8"));
System.out.println(map.values());
}
一个字符串有多个img如何取出来 ?
最新推荐文章于 2022-01-03 20:41:30 发布