Java中使用正则表达式获取网页中所有图片的路径
/**
*从HTML源码中提取图片路径,最后以一个String类型的List返回,如果不包含任何图片,则返回一个size=0的List
*需要注意的是,此方法只会提取以下格式的图片:.jpg|.bmp|.eps|.gif|.mif|.miff|.png|.tif|.tiff|.svg|.wmf|.jpe|.jpeg|.dib|.ico|.tga|.cut|.pic
*@paramhtmlCodeHTML源码
*@return<img>标签src属性指向的图片地址的List集合
*@authorCarlHe
*/
publicstaticList<String>getImageSrc(StringhtmlCode){
List<String>imageSrcList=newArrayList<String>();
Patternp=Pattern.compile("<img//b[^>]*//bsrc//b//s*=//s*('|/")?([^'/"/n/r/f>]+(//.jpg|//.bmp|//.eps|//.gif|//.mif|//.miff|//.png|//.tif|//.tiff|//.svg|//.wmf|//.jpe|//.jpeg|//.dib|//.ico|//.tga|//.cut|//.pic)//b)[^>]*>",Pattern.CASE_INSENSITIVE);
Matcherm=p.matcher(htmlCode);
Stringquote=null;
Stringsrc=null;
while(m.find()){
quote=m.group(1);
src=(quote==null||quote.trim().length()==0)?m.group(2).split("//s+")[0]:m.group(2);
imageSrcList.add(src);
}
returnimageSrcList;
}热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短