我有段格式的字符串,我要取出里面的img标签里src的值 并替换新的地址
<p>12345678</p><p>3<img src="http://192.168.20.12/PIM/wave/BC17062302747013/AWBB7339959/07-Printscreen/tmall/DETAIL/CP-3.jpg">
<img src="http://192.168.20.12/PIM/wave/BC1702747013/AWBB7339959/07-Printscreen/tmall/DETAIL/XJT-1.jpg"></p>
推荐此博客:http://blog.csdn.net/huweijun_2012/article/details/51900814?locationNum=2&fps=1
楼主可以参考下
public class Regex {
public static void main(String[] args) {
//测试用文本 str
String str="";
String regex=" System.out.println(getRegexResult(str,regex).toString());
}
public static List getRegexResult(String sb,String regex){
List resultlist=new ArrayList();
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(sb.toString());
while(m.find()){
resultlist.add(m.group(1));
}
return resultlist;
}
}
//===============
//运行结果 存放在List里面 [http://192.168.20.121//sssssss1.jpg]
String str="<p>12345678</p><p>3<img src=\"http://192.168.20.12/PIM/wave/BC17062302747013/AWBB7339959/07-Printscreen/tmall/DETAIL/CP-3.jpg\">"+
"<img src=\"http://192.168.20.12/PIM/wave/BC1702747013/AWBB7339959/07-Printscreen/tmall/DETAIL/XJT-1.jpg\"></p>";
Pattern p=Pattern.compile("src=\"([^\"]+)\"");
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}
结果在下面
http://192.168.20.12/PIM/wave/BC17062302747013/AWBB7339959/07-Printscreen/tmall/DETAIL/CP-3.jpg
http://192.168.20.12/PIM/wave/BC1702747013/AWBB7339959/07-Printscreen/tmall/DETAIL/XJT-1.jpg
public class Regex {
public static void main(String[] args) {
//
String str="";
String regex=" System.out.println(getRegexResult(str,regex));
}
public static List getRegexResult(String sb,String regex){
//String regex=" List resultlist=new ArrayList();
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(sb.toString());
while(m.find()){
resultlist.add(m.group(1));
}
return resultlist;
}
}
String str="<img src=\"http://192.168.20.12/PIM/wave1.jpg\">";
String regex="<img src=\"([\\w\\s.:/]+?)\"";
System.out.println(getRegexResult(str,regex));
//直接贴代码被屏蔽了,在你宝地再发下
public class Regex {
public static void main(String[] args) {
//<img src="http://192.168.20.12/PIM/wave1.jpg">
String str="<img src=\"http://192.168.20.12/PIM/wave1.jpg\">";
String regex="<img src=\"([\\w\\s.:/]+?)\"";
System.out.println(getRegexResult(str,regex));
}
public static List<String> getRegexResult(String sb,String regex){
//String regex="<img src=\"([\\w\\s.:/]+?)\"";
List<String> resultlist=new ArrayList<String>();
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(sb.toString());
while(m.find()){
resultlist.add(m.group(1));
}
return resultlist;
}
}
regex="<img\s*src=\"(.*)\";
获取group(1)即可