要求能匹配出类似如下<a>标签中的链接
<a href="kctsshow.asp?id=103" title=2009年7月9日发布>
<a href='kctsshow.asp?id=103' title=2009年7月9日发布>
<a href="http://218.22.221.166:8080/index.jsp?gid=72">
<a href="http://192.168.0.81/worksys/" target="_blank">
<a href = " http://192.168.0.81/worksys/ " target = "_blank " >我自己写了一个,但是匹配的结果没有达到预期;它总是以最大方式匹配所以出现的结果向下面这样:
public void i(){
String s = "<a\\s+href\\s*=\\s*\"([^>]*\\s*)\"[^>]*>";
String tmpStr = "<a href=\"http://192.168.0.80:8080/index.jsp\" target=\"_blank\">";
Pattern p = Pattern.compile(s,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(tmpStr);
while(m.find()){
System.out.println(m.group(1));
}
}//结果:http://192.168.0.80:8080/index.jsp" target="_blank
//我想要的是:http://192.168.0.80:8080/index.jsp
<a href="kctsshow.asp?id=103" title=2009年7月9日发布>
<a href='kctsshow.asp?id=103' title=2009年7月9日发布>
<a href="http://218.22.221.166:8080/index.jsp?gid=72">
<a href="http://192.168.0.81/worksys/" target="_blank">
<a href = " http://192.168.0.81/worksys/ " target = "_blank " >我自己写了一个,但是匹配的结果没有达到预期;它总是以最大方式匹配所以出现的结果向下面这样:
public void i(){
String s = "<a\\s+href\\s*=\\s*\"([^>]*\\s*)\"[^>]*>";
String tmpStr = "<a href=\"http://192.168.0.80:8080/index.jsp\" target=\"_blank\">";
Pattern p = Pattern.compile(s,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(tmpStr);
while(m.find()){
System.out.println(m.group(1));
}
}//结果:http://192.168.0.80:8080/index.jsp" target="_blank
//我想要的是:http://192.168.0.80:8080/index.jsp
取出1组的数据就行了
说明一下因为实在java中我将表达式改为:<a.*href\\s*=\\s*[\"|'](.*)[\"|'].*>
String s = "<a\\s+href\\s*=\\s*\"([^>]*?\\s*)\"[^>]*>";
//[^>]*就是在你的这一块后面加一个?就行了
//贪婪和非贪婪的区别