<a href="/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820">[提交]</a>想在这句超链接里提取出proj_id和task_id的值。不知道该如何写正则。。
效果就是通过java获取html页面中的某些数据,为我所用。例如这个:public static void main(String[] args) throws MalformedURLException,Exception {
        HttpURLConnection conn = (HttpURLConnection) new URL("http://bbs.csdn.net/forums/Java").openConnection();
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        BufferedReader br = new BufferedReader(new InputStreamReader(bis,"utf-8"));
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.contains("target=\"_blank\" title=\"")) {
                System.out.println(line.replaceAll(".*?title=\"([^\"].+?)\".*+", "$1"));
            }
        }
        br.close();
    }求高人帮忙写一个。。拜托了。

解决方案 »

  1.   

    public static Map match(String source, String element, String attr) {
    Map result = new LinkedHashMap();
    String reg = "<" + element + "[^<>]*?\\s"+ attr+ "=['\"]?(.*?)['\"].*?>";
    Matcher m = Pattern.compile(reg).matcher(source);
    while (m.find()) {
    String key = m.group(0);
    String value = m.group(1);
    result.put(key, value);
    }
    return result;
    } public static void main(String[] args) {
     String source ="<a href='/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820'>[提交]</a>";
     Map map = match(source, "a", "href");
     Set<Map.Entry> set = map.entrySet();
            for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) {
                Map.Entry entry = (Map.Entry) it.next();
                String []s=entry.getValue().toString().split("\\&");
                System.out.println("proj_id="+s[1].split("=")[1]);
                System.out.println("task_id="+s[2].split("=")[1]);
            }

    }
      

  2.   

    想用我1楼贴出来的方法。。
    从html中的获取信息,html页面中的这个超链接包含了我想要的东西,就是这个
    <a href="/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820">
    然后我将proj_id和task_id获取该如何写呢?
      

  3.   

    String url="<a href=\"/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820\">";
    String result="";
    Pattern p=Pattern.compile("task_id=(.*?)\"");
    Matcher m=p.matcher(url);
    while(m.find()){

    result=m.group(1);
    }
    System.out.println(result);
      

  4.   

    <table id="ProjTask" class="">
    <thead>
    <tr>
    <th><span id='alls' style="cursor:hand"  onclick="selectAll('formName','id');">全选</span></th>
    <th>序号</th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=2">项目名称</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=3">任务名称</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=4">计划开始时间</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=5">计划完成时间</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=6">实际开始时间</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=7">实际完成时间</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=8">计划工时</a></th>
    <th class="sortable">
    <a href="MyTaskAction.do?d-3958256-o=2&amp;method=getMyTaskList&amp;method=getMyTaskList&amp;d-3958256-s=9">任务状态</a></th>
    <th>操作</th></tr></thead>
    <tbody>
    <tr class="odd">
    <td style="width:5px">
      
       <input name="hidemp" type="hidden" class="textbox_short" value="819">
      </td>
    <td style="width:20px">1</td>
    <td style="width:80px">-----</td>
    <td style="width:80px">-----</td>
    <td style="width:80px">2013-04-07</td>
    <td style="width:80px">2013-04-12</td>
    <td style="width:80px">2013-04-07</td>
    <td style="width:80px"></td>
    <td style="width:40px">48.00</td>
    <td style="width:100px">已接受处理中</td>
    <td style="width:80px">
      
       <a href="/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820">[提交]</a>
       
       <a href="#" onClick="openPro(819,131820)">[查看]</a>
      </td></tr>
    我是将这样一HTML页面获取,然后从里面查找proj_id 和task_id 
    和与之相关的就是<a href="/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820">这句话。
    我的想法是如何这样获取到proj_id 和task_id
      

  5.   


     String source ="<a href=\"/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820\">[提交]</a>";         
            if (source.contains("a href=\"")) {
                 System.out.println(source.replaceAll(".*?&(.+?)&(.+?)\".*+", "$1$2"));            }
      

  6.   

    截取分成两个步骤,第一步是
    找到:<a href="/pms-web/DealTaskList.do?method=getDealTaskList&proj_id=819&task_id=131820">
    这句话
    第二步就是我发的那一小段代码,从这句话里面提取信息。
    至于第一步,不就是5楼的代码么