<a href="http://csdn.net"><img src="a.jpg"/></a>
<img src="b.jpg"/>
捕获b.jpg
5 个解决方案
#1
求高手指教。。
#2
看到带有高手字样的,向来都自觉绕路
#3
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));
}
#4
使用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"));
}
#5
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));
}
#1
求高手指教。。
#2
看到带有高手字样的,向来都自觉绕路
#3
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));
}
#4
使用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"));
}
#5
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));
}