截取a标签,获取a标签href 中的链接,获取,href 中的文本值,替换

    
新任务信息:<br/>型号任务:【<a href='/sysware/task/wbs/wbsTaskMain.sitemesh?navigationHandlerBeanName=singleProjectMenuHandler&displayMenu=true&centerId=-1&currentMenuId=C97C34F16BD3E0CFE040007F01003309&taskId=82DC3AB8AC1C4525A376B2AE6B38D4BA' style='text-decoration:underline;color:blue;' target='_blank'>型号项目/0718/</a>】<br/>任务:【<a href='/sysware/task/wbs/taskTabs.sitemesh?currentMenuId=C96BB3B5C4B75752E040007F01001FF7&navigationHandlerBeanName=myTaskMenuHandler&displayMenu=true&taskId=5C6B0F79640144129E49F2C59B5F9FC1' style='text-decoration:underline;color:blue;' target='_blank'>a/a(校对)/</a>】。





private void replaceA(MessageVo vo) {
        //获取taskId,判断是如果审签任务,不要链接
        String countAhref =vo.getMessageContent();
        String aString =countAhref.substring(countAhref.lastIndexOf("【"), countAhref.lastIndexOf("】"));
        String str = "<a href='(.+?)'";
        //Pattern p = Pattern.compile("<a[^>]*>([^<]*)</a>"); 
        Pattern p = Pattern.compile(str); 
         Matcher m = p.matcher(aString);
         String string="" ;
          while(m.find()) { 
              string =m.group(1);//获取字符串,获取 href 链接 /sysware/task/wbs/taskTabs.sitemesh?currentMenuId=C96BB3B5C4B75752E040007F01001FF7&navigationHandlerBeanName=myTaskMenuHandler&displayMenu=true&taskId=5C6B0F79640144129E49F2C59B5F9FC1
          }
         String taskId = string.substring((string.lastIndexOf("="))+1, string.length());//获取taskId
         TaskObject taskObject =this.taskObjectService.getTaskObjectById(taskId);
         if(!SyswareUtil.isEmpty(taskObject)){
             if(taskObject.getTaskFlag().equals("approvalTask")){
                 //去除链接
                 String n =countAhref.substring(countAhref.lastIndexOf("【"), countAhref.lastIndexOf("】")+1);
                 Pattern p2 = Pattern.compile("<a[^>]*>([^<]*)</a>"); //获取a标签中的内容
                 Matcher m2 = p2.matcher(n);
                 String string2="" ;
                  while(m2.find()) { 
                      string2 =m2.group(1);//获取a标签中的内容
                      vo.setMessageContent(countAhref.replace(n, string2));
                  }
             }
         }
    }
 

  

posted @ 2018-07-19 09:37  赤子之心_timefast  阅读(2461)  评论(0编辑  收藏  举报