这是解析HTML中图片路径的正则表达式。
//<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>
String rgrEpn="<img\\b[^<>]*?\\bsrc[\\s\\t\\r\\n]*=[\\s\\t\\r\\n]*[\"\"']?[\\s\\t\\r\\n]*(?<imgUrl>[^\\s\\t\\r\\n\"\"'<>]*)[^<>]*?/?[\\s\\t\\r\\n]*>";
Pattern pattern=Pattern.compile(rgrEpn);
Matcher matcher=pattern.matcher(content);
while(matcher.find()){
String ime=matcher.group();
System.out.println(ime);
}
<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>

                                                                                    ^

解决方案 »

  1.   

    src\\s*=\\s*(\"[^\"]*\"|'[^']*'|[^\\s>]+)
      

  2.   

    你既然只是捕获img标签,而不是校验,不用这样麻烦啊。<img[^<>]+ \?\s*>把他当成xml解析,利用path也许更准确。
      

  3.   

    大家帮我看看吧,是不是正则表达式放在java里要用其它的转义符号代替?主要功能是可以提取下面这些HTML代码中的图片。能帮我看看我上面的代码哪里出的错误吗?<div class=\"m_l_list_hot\"><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200909/152449.htm\" title=\"C#数组基础介绍与操作详解\"><img src=\"http://images.51cto.com/files/uploadimg/20090917/150811926.jpg\" alt=\"C#数组基础介绍与操作详解\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200704/46251.htm\" title=\"C#实用基础教程\"><img src=\"http://images.51cto.com/files/uploadimg/20070420/184031793.gif\" alt=\"C#实用基础教程\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200509/4390.htm\" title=\"C#技术开发指南\"><img src=\"http://images.51cto.com/files/uploadimg/20070901/205733160.gif\" alt=\"C#技术开发指南\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://book.51cto.com/art/201003/187320.htm\" title=\"SQL Server 2008编程入门经典\"><img src=\"http://images.51cto.com/files/uploadimg/20100308/115049178.gif\" alt=\"SQL Server 2008编程入门经典\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://book.51cto.com/art/201003/187121.htm\" title=\"Web编程入门经典--HTML、XHTML和CSS(第2版)\"><img src=\"http://images.51cto.com/files/uploadimg/20100307/113131795.gif\" alt=\"Web编程入门经典--HTML、XHTML和CSS(第2版)\" width=\"100\" height=\"80\" border=\"0\" /></a></div></div>
      

  4.   

    <img\s*src=\\"([^<> ]+)" 试试这个正则表达式匹配结果
     <img src=\"http://images.51cto.com/files/uploadimg/20090917/150811926.jpg\"
    <img src=\"http://images.51cto.com/files/uploadimg/20070420/184031793.gif\"
    <img src=\"http://images.51cto.com/files/uploadimg/20070901/205733160.gif\"
    <img src=\"http://images.51cto.com/files/uploadimg/20100308/115049178.gif\"
    <img src=\"http://images.51cto.com/files/uploadimg/20100307/113131795.gif\"
      

  5.   

    嗯,不是不可以。单引号、没有单双引号都就应该可以匹配。img 与 src之间有可能出现空格、Tab、回车等。总之,我之前那个表达式已经写的很全了。我想在那个表达式基础上解析,大家还是帮帮指出一下我那个表达式在Java中解析时出的问题吧。
      

  6.   

    java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 84总报这个异常,网上搜索,也没人能指出这个结果的原因。
      

  7.   


    String content = "<div class=\"m_l_list_hot\"><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200909/152449.htm\" title=\"C#数组基础介绍与操作详解\"><img src=\"http://images.51cto.com/files/uploadimg/20090917/150811926.jpg\" alt=\"C#数组基础介绍与操作详解\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200704/46251.htm\" title=\"C#实用基础教程\"><img src=\"http://images.51cto.com/files/uploadimg/20070420/184031793.gif\" alt=\"C#实用基础教程\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://developer.51cto.com/art/200509/4390.htm\" title=\"C#技术开发指南\"><img src=\"http://images.51cto.com/files/uploadimg/20070901/205733160.gif\" alt=\"C#技术开发指南\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://book.51cto.com/art/201003/187320.htm\" title=\"SQL Server 2008编程入门经典\"><img src=\"http://images.51cto.com/files/uploadimg/20100308/115049178.gif\" alt=\"SQL Server 2008编程入门经典\" width=\"100\" height=\"80\" border=\"0\" /></a></div><div class=\"m_l_list_hot1\"><a href=\"http://book.51cto.com/art/201003/187121.htm\" title=\"Web编程入门经典--HTML、XHTML和CSS(第2版)\"><img src=\"http://images.51cto.com/files/uploadimg/20100307/113131795.gif\" alt=\"Web编程入门经典--HTML、XHTML和CSS(第2版)\" width=\"100\" height=\"80\" border=\"0\" /></a></div></div>";
    String rgrEpn="<img[^<>]+(src\\s*=[^<>]+)(?<=gif\"|jpg\")[^<>]+ /?\\s*>";
    Pattern pattern=Pattern.compile(rgrEpn);
    Matcher matcher=pattern.matcher(content);
    while(matcher.find()){
    String ime=matcher.group();
    System.out.println(ime);
    System.out.println(matcher.group(1));试了下 这样应该可以。如果有其他格式的图片,继续加在上面。
      

  8.   

    String rgrEpn="<img[^<>]+src\\s*=([^<>\\s]+)[^<>]+ /?\\s*>";改良下 这样更好。但是图片路径中就不能出现空白字符了,看你自己取舍了
      

  9.   

    可不可以不改变我的表达式,那个表达式放在C#里没有问题,在java中怎么会有问题呢?