求在一段html中提取所有链接的正则

fx1989529 2009-03-10 05:22:08
文本是这样的

<div class="mid2">
<br/><br><center>
<a href="/affix_pdf/images/2007/12/1197863159.jpg" rel="lightbox">
<a href="/affix_pdf/images/2007/12/1197863159.jpg" rel="lightbox">
<img src="/affix_pdf/images/2007/12/1197863159.jpg" width=420 border="0">
</a></center></div>


中间有两个超链接,怎么把这两个链接用正则提取出来....
...全文
83 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fulianglove 2009-03-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Landor2004 的回复:]
你没调用find呀

Java codewhile (m.find()) {
System.out.println(m.group());
}
[/Quote]
Landor2004 2009-03-10
  • 打赏
  • 举报
回复
你没调用find呀
while (m.find()) {
System.out.println(m.group());
}
如一宝宝 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luxu001207 的回复:]
你要取地址还是取整个链接?
取整个链接: <a[^>]*>[\s\S]* </a>
取地址:\s*href\s*=[\"'\s]?([^\s\"']*)[\"'\s]?
[/Quote]
按你你的正则还是不对啊,这是我的代码

String template = "<div class=\"mid2\"><br/><br><center><a href=\"/affix_pdf/images/2007/12/1197863159.jpg\" rel=\"lightbox\"><a href=\"/affix_pdf/images/2007/12/1197863159.jpg\" rel=\"lightbox\"><img src=\"/affix_pdf/images/2007/12/1197863159.jpg\"></a></center></div>";
Pattern p = Pattern.compile("\\s*href\\s*=[\\\"'\\s]?([^\\s\\\"']*)[\\\"'\\s]?");
Matcher m = p.matcher(template);

System.out.println(m.group(0));
Atai-Lu 2009-03-10
  • 打赏
  • 举报
回复
你要取地址还是取整个链接?
取整个链接:<a[^>]*>[\s\S]*</a>
取地址:\s*href\s*=[\"'\s]?([^\s\"']*)[\"'\s]?

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧