比如:
<a href="http://csdn.net"><img src="a.jpg"/></a>
<img src="b.jpg"/>
捕获b.jpg

解决方案 »

  1.   

    try... String test = "<a href=\"http://csdn.net\"><img src=\"a.jpg\"/></a>\n"
    + "<img src=\"b.jpg\"/>";
    String pattern = "(?is)<img[^>]*?src=\"([^\"]*)\"[^>]*>(?!((?!</?a\\b).)*</a>)";
    Matcher m = Pattern.compile(pattern).matcher(test);
    while(m.find())
    {
    System.out.println( m.group(1));
    }
      

  2.   

    使用jsoup 
    import org.jsoup.nodes.*;
    import org.jsoup.select.*;
    import org.jsoup.*; String input = 
        "<html>" 
        + "<head><title>example</title></head>"
        + "<body>"
        + "<a href=\"http://csdn.net\"><img src=\"a.jpg\"/></a>"
        + "<img src=\"b.jpg\"/>"
        + "</body>"
        + "</html>";
    Document doc = Jsoup.parse(input);
    Elements allImages = doc.select("img");
    Elements otherImages = doc.select("a > img");
    allImages.removeAll(otherImages);
    for(Element e : allImages){
        System.out.println(e.attr("src"));
    }
      

  3.   


    String test = "<a href=\"http://csdn.net\"><img src=\"a.jpg\"/></a>\n"
     +"<a><img src=\"a.jpg\"/></a>\n"
     +"<a href=\"\"><img src=\"a.jpg\"/></a>\n"
            + "<img src=\"b.jpg\"/>";
            String pattern = "<img\\s+src=\"([\\w+|\\.]*)\"\\/\\>(?<!<a)(?!</a>)";
            Matcher m = Pattern.compile(pattern).matcher(test);
            while(m.find()){
                System.out.println( m.group(1));
            }