日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java應(yīng)用集錦3:正則表達(dá)式 excel操作之jxl (轉(zhuǎn))

 臨風(fēng)笛 2010-07-14

最近項(xiàng)目開(kāi)發(fā)中對(duì)excel操作比較頻繁,并結(jié)合正則表達(dá)式進(jìn)行了一些處理,整理一下.


1.正則表達(dá)式常用方法

Java代碼 復(fù)制代碼
  1. /**  
  2.      * 在第一個(gè)字符串中查找匹配字符串的個(gè)數(shù)  
  3.      * @param str    
  4.      * @param regexStr  
  5.      * @return  
  6.      */  
  7.     public static int count(String str,String regexStr){   
  8.         int count = 0;   
  9.         Pattern pt = Pattern.compile(regexStr);   
  10.         Matcher m = pt.matcher(str);   
  11.         int start = 0;   
  12.         while(m.find()){   
  13.             count++;   
  14.             str = str.replaceFirst(regexStr, "");   
  15.         }   
  16.         return count;   
  17.     }   
  18.   
  19. /**  
  20.      * 根據(jù)正則表達(dá)式分割str字符串成為一個(gè)一個(gè)的小的單元!  
  21.          * (實(shí)際使用:在一個(gè)類似語(yǔ)法分析的模塊中發(fā)揮重要作用)  
  22.      * 例如:3+5*4  根據(jù)正則表達(dá)式+-\*  分割成數(shù)組  3,+,5,*,4    
  23.      * @param str  
  24.      * @param regexStr  
  25.      * @return  
  26.      */  
  27.     public static List splitByStr(String str,String regexStr){   
  28.         List temp = new ArrayList();   
  29.         Pattern pt = Pattern.compile(regexStr);   
  30.         Matcher m = pt.matcher(str);   
  31.         int start = 0;   
  32.         while(m.find()){   
  33.             //去掉下面的字符串中為空串的情況!   
  34.             if(m.start()!=start)   
  35.                 temp.add(str.substring(start, m.start()));   
  36.             temp.add(str.substring(m.start(),m.end()));   
  37.             start = m.end();   
  38.         }   
  39.         temp.add(str.substring(start));   
  40.         return temp;   
  41.     }   
  42.   
  43.       /**  
  44.      * 檢查是否含有指定的正則表達(dá)式匹配的子串.  
  45.      * @param str 目標(biāo)字符串  
  46.      * @param regex 正則表達(dá)式,如果正則表達(dá)式含有"^......$"就是查找整個(gè)字符串對(duì)象是否符合正則表達(dá)式.  
  47.      * @return  
  48.      */  
  49.     public static boolean checkInclude(String str,String regex){   
  50.          Pattern pattern = Pattern.compile(regex);   
  51.          Matcher matcher = null;   
  52.          matcher = pattern.matcher(str);   
  53.          return matcher.find();   
  54.     }   
  55.            
  56.     /**  
  57.      * 方法字符串中符合正則表達(dá)式的子串的集合.  
  58.      * @param str  
  59.      * @param regex  
  60.      * @return  
  61.      */  
  62.     public static List getRightSubStr(String str, String regex) {   
  63.         List ans = new ArrayList();   
  64.         Pattern pattern = Pattern.compile(regex);   
  65.         Matcher matcher = pattern.matcher(str);   
  66.         while (matcher.find()) {   
  67.             //注意要下面的goup()函數(shù)中可以含有數(shù)字,表示查找得到正則表達(dá)式中的goup匹配串.   
  68.             ans.add(matcher.group());   
  69.             System.out.println("找到匹配的字符串 \"" + matcher.group()   
  70.                     + "\" 開(kāi)始于 " + matcher.start()   
  71.                     + " 結(jié)束于 " + matcher.end() + ".");   
  72.         }   
  73.         return ans;   
  74.     }  

 下面是java正則表達(dá)式經(jīng)常使用的一些方法和說(shuō)明:

Java代碼 復(fù)制代碼
  1. 1)使用matches方法快速建設(shè)是否表示給定的輸入字符串:Pattern.matches("\\d","1")返回true  
  2. 2)split(string)使用方法:Pattern.compile(":").split("one:two:three:four:five");  返回:解析出“one two three four five”單詞   
  3. 再比如使用數(shù)字作為一個(gè)分割字符串的方法:(注意下面的\\d不是正則表達(dá)式,而是前面加了一個(gè)轉(zhuǎn)義符號(hào)\)   
  4. Pattern.compile("\\d").split("one9two4three7four1five");也返回相同的結(jié)果。。   
  5. 3)在String類中有的幾個(gè)與Pattern類似的方法:   
  6. public boolean matches(String regex):   
  7. public String[] split(String regex, int limit):   
  8. public String[] split(String regex):   
  9. public String replace(CharSequence target,CharSequence replacement):   
  10. 4) Matcher 類中其他一些有用的方法   
  11. 索引方法   
  12.   索引方法(index methods)提供了一些正好在輸入字符串中發(fā)現(xiàn)匹配的索引值:   
  13.   public int start():返回之前匹配的開(kāi)始索引。   
  14.   public int start(int group):返回之前匹配操作中通過(guò)給定組所捕獲序列的開(kāi)始索引。   
  15.   public int end(): 返回最后匹配字符后的偏移量。   
  16.   public int end(int group): 返回之前匹配操作中通過(guò)給定組所捕獲序列的最后字符之后的偏移量。    
  17. 研究方法   
  18.   研究方法(study methods)回顧輸入的字符串,并且返回一個(gè)用于指示是否找到模式的布爾值。   
  19.   public boolean lookingAt(): 嘗試從區(qū)域開(kāi)頭處開(kāi)始,輸入序列與該模式匹配。   
  20.   public boolean find(): 嘗試地尋找輸入序列中,匹配模式的下一個(gè)子序列。   
  21.   public boolean find(int start): 重置匹配器,然后從指定的索引處開(kāi)始,嘗試地尋找輸入序列中,匹配模式的下一個(gè)子序列。   
  22.   public boolean matches(): 嘗試將整個(gè)區(qū)域與模式進(jìn)行匹配    
  23. 替換方法   
  24.   替換方法(replacement methods)用于在輸入的字符串中替換文本有用處的方法。   
  25.   public Matcher appendReplacement(StringBuffer sb, String replacement):實(shí)現(xiàn)非結(jié)尾處的增加和替換操作。   
  26.   public StringBuffer appendTail(StringBuffer sb):實(shí)現(xiàn)結(jié)尾處的增加和替換操作。   
  27.   public String replaceAll(String replacement):使用給定的替換字符串來(lái)替換輸入序列中匹配模式的每一個(gè)子序列。   
  28.   public String replaceFirst(String replacement):使用給定的替換字符串來(lái)替換輸入序列中匹配模式的第一個(gè)子序列。   
  29.   public static String quoteReplacement(String s):返回指定字符串的字面值來(lái)替換字符串。這個(gè)方法會(huì)生成一個(gè)字符串,用作 Matcher 的 appendReplacement 方法中的字面值替換 s。所產(chǎn)生的字符串將與作為字面值序列的 s 中的字符序列匹配。斜線(\)和美元符號(hào)($)將不再有特殊意義了。   

   正則表達(dá)式基礎(chǔ):

字符類
[abc]   a, b 或 c(簡(jiǎn)單類)
[^abc]         除 a, b 或 c 之外的任意字符(取反)
[a-zA-Z]           a 到 z,或 A 到 Z,包括(范圍)
[a-d[m-p]]              a 到 d,或 m 到 p---等價(jià)于[a-dm-p](并集) (特別注意這里的并集的方式啊,很特別?。。?br>[a-z&&[def]]               d,e 或 f(交集)
[a-z&&[^bc]]              除 b 和 c 之外的 a 到 z 字符等價(jià)于[ad-z](差集)
[a-z&&[^m-p]]                     a 到 z,并且不包括 m 到 p等價(jià)于[a-lq-z](差集)

 

預(yù)定義字符類
.         任何字符(匹配或者不匹配行結(jié)束符)
\d         數(shù)字字符:[0-9]
\t        tab鍵
\D         非數(shù)字字符:[^0-9]
\s         空白字符:[\t\n\x0B\f\r]
\S         非空白字符:[^\s]
\w         單詞字符:[a-zA-Z_0-9]
\W         非單詞字符:[^\w]

 

邊界匹配器
^         行首
$         行尾
\b         單詞邊界
\B         非單詞邊界
\A         輸入的開(kāi)頭
\G         上一個(gè)匹配的結(jié)尾
\Z         輸入的結(jié)尾,僅用于最后的結(jié)束符(如果有的話)
\z         輸入的結(jié)尾

 2.使用jxl進(jìn)行exlce的基本操作

下面基礎(chǔ)代碼來(lái)自于網(wǎng)絡(luò):

Java代碼 復(fù)制代碼
  1. import java.io.File;     
  2. import java.io.FileOutputStream;     
  3. import java.io.OutputStream;     
  4. import java.util.ArrayList;     
  5. import java.util.Date;     
  6.     
  7. import jxl.Cell;     
  8. import jxl.CellType;     
  9. import jxl.Sheet;     
  10. import jxl.Workbook;     
  11. import