Jslfl【软件开发技术笔记】

正则提取html代码中img的图片地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] s){
  String htmlStr = <a href="UploadImg/2013412111411.jpg"><img alt="" src="UploadImg/2013412111411.jpg" width="180" height="135" border="0" /></a>";
  String img="
";
  Pattern p_image;
  Matcher m_image;
  List pics = new ArrayList();

  String regEx_img = "
<img.*src=(.*?)[^>]*?>"; //图片链接地址
  p_image = Pattern.compile(regEx_img,Pattern.CASE_INSENSITIVE);
  m_image = p_image.matcher(htmlStr);
  while(m_image.find()){
    img = img + "
," + m_image.group();
    Matcher m = Pattern.compile("
src="?(.*?)("|>|\\s+)").matcher(img); //匹配src
    while(m.find()){
      pics.add(m.group(1));
    }
  }
  for(String ss:pics){
     System.out.println(ss);
  }
}

转http://blog.sina.com.cn/s/blog_49dffa3b0100tkg5.html

Comments are currently closed.