求救!!正则表达式,祝大家元旦快乐,新的一年快乐。。

xr396464010 2010-01-01 12:12:50
首先在这里祝大家新年快乐:
然后我这里有个小问题想请正则高手帮忙解决下:
下面一段是源码:我要匹配中间的内容如下:
1 日用百货首页
2 个护化妆
3 唇膏唇彩
4 曼秀雷敦
最好以 <div id="Position"[^<>]*>这个指定的DIV ID开始 因为在这个DIV 开始之前会有很多的HTML 或者是其它的DIV
<div id="Position" class="margin_b6"><a href="http://www.360buy.com/">首页</a> > <a href = "http://www.360buy.com/jdlife.aspx">日用百货首页</a> > <a href="http://www.360buy.com/products/911-1125-000-0-0-0-0-0-0-0-1-1-1.html">个护化妆</a> > <a href="http://www.360buy.com/products/911-1125-1134-0-0-0-0-0-0-0-1-1-1.html">唇膏唇彩</a> > <a href="http://www.360buy.com/brands/1134,%e6%9b%bc%e7%a7%80%e9%9b%b7%e6%95%a6.html">曼秀雷敦</a></div>

还请大家不吝赐教:
...全文
90 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
micsolaris 2010-06-10
  • 打赏
  • 举报
回复

/**
首先在这里祝大家新年快乐:
然后我这里有个小问题想请正则高手帮忙解决下:
下面一段是源码:我要匹配中间的内容如下:
1 日用百货首页
2 个护化妆
3 唇膏唇彩
4 曼秀雷敦
最好以 <div id="Position"[^<>]*>这个指定的DIV ID开始 因为在这个DIV 开始之前会有很多的HTML 或者是其它的DIV
<div id="Position" class="margin_b6"><a href="http://www.360buy.com/">首页</a> > <a href = "http://www.360buy.com/jdlife.aspx">日用百货首页</a> > <a href="http://www.360buy.com/products/911-1125-000-0-0-0-0-0-0-0-1-1-1.html">个护化妆</a> > <a href="http://www.360buy.com/products/911-1125-1134-0-0-0-0-0-0-0-1-1-1.html">唇膏唇彩</a> > <a href="http://www.360buy.com/brands/1134,%e6%9b%bc%e7%a7%80%e9%9b%b7%e6%95%a6.html">曼秀雷敦</a></div>

还请大家不吝赐教:
*/

//package com.ricky.www;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;

public class Test{
public static void main(String[] args){
String content = "<div id=\"Position\" cass=\"margin_b6\"><a href=\"http://www.360buy.com/\">首页</a> > <a href = \"http://www.360buy.com/jdlife.aspx\">日用百货首页</a> > <a href=\"http://www.360buy.com/products/911-1125-000-0-0-0-0-0-0-0-1-1-1.html\">个护化妆</a> > <a href=\"http://www.360buy.com/products/911-1125-1134-0-0-0-0-0-0-0-1-1-1.html\">唇膏唇彩</a> > <a href=\"http://www.360buy.com/brands/1134,%e6%9b%bc%e7%a7%80%e9%9b%b7%e6%95%a6.html\">曼秀雷敦</a></div>";
for(Object str : handle(content).toArray()){
System.out.println(str);
}
}

public static ArrayList<String> handle(String content){
ArrayList<String> list = new ArrayList<String>();
String regex = "<a[^>]*>(.*?)</a>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
while(matcher.find()){
list.add(matcher.group(1));
}

return list;
}
}
马老虎 2010-01-01
  • 打赏
  • 举报
回复
来了!新年快乐!
DrinkBeer 2010-01-01
  • 打赏
  • 举报
回复
学习正则表达式中,来顶一个~!

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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