正则表达式替换url
请教各位网友,下面这个正则表达式改怎样写?
原始代码是这样:
<div style="background-color: #cccccc; margin: 5px auto; width: 215px; height: 215px">
<a href="http://XXX/t?id=XXX" target="_blank" title="XXX"><img src="http://XXX/aaa.jpg" style="width: 213px; height: 213px" /> </a></div>
<div style="line-height: 14px; background-color: #fff; margin: 5px auto; width: 215px; height: 14px">
String html = ...;
Document doc = Jsoup.parseBodyFragment(html);
Element a = doc.select("a").first();
a.attr("href", "http://www.baidu.com");
String result = doc.outerHtml();
<html>
<head></head>
<body>
<div style="background-color: #cccccc; margin: 5px auto; width: 215px; height: 215px">
<a href="http://www.baidu.com" target="_blank" title="XXX"> <img src="http://XXX/aaa.jpg" style="width: 213px; height: 213px" /> </a>
</div>
<div style="line-height: 14px; background-color: #fff; margin: 5px auto; width: 215px; height: 14px"></div>
</body>
</html>
String source = "<div style="background-color: #cccccc; margin: 5px auto; width: 215px; height: 215px"><a href="http://XXX/t?id=XXX" target="_blank" title="XXX"><img src="http://XXX/aaa.jpg" style="width: 213px; height: 213px" /> </a></div><div style="line-height: 14px; background-color: #fff; margin: 5px auto; width: 215px; height: 14px">";
String reg = "<a[^<>]*?\\shref=['"]?(.*?)['"].*?>";
Matcher m = Pattern.compile(reg).matcher(source);
while (m.find()) {
String value = m.group(1);
System.out.println(value);
System.out.println(source.replace(value, "www.xxoo.com"));
}