111,120
社区成员
发帖
与我相关
我的任务
分享
/**
首先在这里祝大家新年快乐:
然后我这里有个小问题想请正则高手帮忙解决下:
下面一段是源码:我要匹配中间的内容如下:
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;
}
}