qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Java處理字符串搜索嵌套結構的方法

           在用Java分析HTML文本時,如果要取出有嵌套結構的節點之間的內容,不能直接用正則表達式來處理,因為Java所帶的正則表達式不支持嵌套結構的描述,雖然Perl、.Net、PHP可以支持。這時可以先用正則表達式找出節點在字符串中的位置,然后對節點進行匹配處理,取出匹配節點之間的內容,實現對嵌套結構的處理。

            例如要從

          1. <pre name="code" class="java">data=<div><div>abcd<div></div><form>
          2. <input type='button' value='submit'/></form></div></div><div>1234</div>

            中取出<div></div>之間的內容,希望返回兩個字符串

          1. <pre name="code" class="java"><div>abcd<div></div><form>
          2. <input type='button' value='submit'/></form></div><pre name="code" class="html">和1234

            源代碼如下:

            為了記錄節點在字符串中的值和位置,先定義一個類,保存這些信息:

          1. public class Tag {  
          2.       
          3.     public Tag(String value, int beginPos, int endPos) {  
          4.         super();  
          5.         this.value = value;  
          6.         this.beginPos = beginPos;  
          7.         this.endPos = endPos;  
          8.     }  
          9.     private String value;  
          10.     private int beginPos;  
          11.     private int endPos;  
          12.     public String getValue() {  
          13.         return value;  
          14.     }  
          15.     public void setValue(String value) {  
          16.         this.value = value;  
          17.     }  
          18.     public int getBeginPos() {  
          19.         return beginPos;  
          20.     }  
          21.     public void setBeginPos(int beginPos) {  
          22.         this.beginPos = beginPos;  
          23.     }  
          24.     public int getEndPos() {  
          25.         return endPos;  
          26.     }  
          27.     public void setEndPos(int endPos) {  
          28.         this.endPos = endPos;  
          29.     }  
          30.       
          31. }

            從字符串中獲取節點之間內容的函數如下:

          1.        /** 
          2.  * 獲取字符串之間的內容,如果包含嵌套,則返回最外層嵌套內容 
          3.  *  
          4.  * @param data       
          5.  * @param stag      起始節點串 
          6.  * @param etag      結束節點串 
          7.  * @return 
          8.  */ 
          9. public List<String> get(String data,String stag, String etag){  
          10.     // 存放起始節點,用于和結束節點匹配 
          11.     Stack<Tag> work = new Stack<Tag>();  
          12.     // 保存所有起始和結束節點 
          13.     List<Tag> allTags = new ArrayList<Tag>();  
          14.       
          15.     // 在元字符前加轉義符 
          16.     String nstag = stag.replaceAll("([\\*\\.\\+\\(\\]\\[\\?\\{\\}\\^\\$\\|\\\\])""\\\\$1");  
          17.     String netag = etag.replaceAll("([\\*\\.\\+\\(\\]\\[\\?\\{\\}\\^\\$\\|\\\\])""\\\\$1");  
          18.       
          19.     String reg = "((?:"+nstag+")|(?:"+netag+"))";  
          20.       
          21.     Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);  
          22.       
          23.     Matcher m = p.matcher(data);  
          24.       
          25.     while(m.find()){  
          26.         Tag tag = new Tag(m.group(0),m.start(),m.end());  
          27.         allTags.add(tag);  
          28.     }  
          29.     // 保存開始結束節點之間的內容,不含節點 
          30.     List<String> result = new ArrayList<String>();  
          31.       
          32.     for(Tag t : allTags){  
          33.         if (stag.equalsIgnoreCase(t.getValue())){  
          34.             work.push(t);  
          35.         }else if(etag.equalsIgnoreCase(t.getValue())){  
          36.             // 如果棧已空,則表示不匹配 
          37.             if (work.empty()){  
          38.                 throw new RuntimeException("pos "+t.getBeginPos()+" tag not match start tag.");  
          39.             }  
          40.             Tag otag = work.pop();  
          41.             // 如果棧為空,則匹配 
          42.             if (work.empty()){  
          43.                 String sub = data.substring(otag.getEndPos(), t.getBeginPos());  
          44.                 result.add(sub);  
          45.             }  
          46.         }  
          47.           
          48.     }  
          49.       
          50.     // 如果此時棧不空,則有不匹配發生 
          51.     if (!work.empty()){  
          52.         Tag t = work.pop();  
          53.         throw new RuntimeException("tag "+t.getValue()+ "not match.");  
          54.     }  
          55.       
          56.     return result;  
          57.       
          58. }

            函數返回節點之間內容串組成的列表。

            例如 調用 get(data,"<div>", "</div>") 返回含有兩個元素的列表,元素分別為

          <div>abcd<div></div><form><input type='button' value='>'/></form></div>, 1234

            需要注意的是如果節點含有正則表達式的元字符,需要在元字符前加轉義符\\,源代碼中第16,17行實現此功能。

          posted on 2012-02-07 15:21 順其自然EVO 閱讀(235) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2012年2月>
          2930311234
          567891011
          12131415161718
          19202122232425
          26272829123
          45678910

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 葵青区| 平江县| 呼伦贝尔市| 江山市| 张家港市| 宁乡县| 盈江县| 探索| 乐陵市| 长宁县| 徐汇区| 泗洪县| 黎川县| 浦城县| 鄄城县| 嵊州市| 赣榆县| 七台河市| 文安县| 印江| 余庆县| 县级市| 娱乐| 柞水县| 南宁市| 桐庐县| 拉萨市| 瑞昌市| 贵阳市| 常熟市| 来宾市| 潞城市| 六安市| 大方县| 淮南市| 会昌县| 庆云县| 永春县| 临泉县| 呼伦贝尔市| 封丘县|