<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    海闊天空

    I'm on my way!
    隨筆 - 17, 文章 - 69, 評論 - 21, 引用 - 0
    數據加載中……

    一個正則表達式工具類

    一個java正規表達式工具類
    類中用到了 jakarta-oro-2.0.jar 包,請大家自己在 apache網站下下載

    在這是junit測試單元類我就不提交了,在main()方法中有幾個小測試,有興趣自己玩吧.

    這個工具類目前主要有25種正規表達式(有些不常用,但那時才仔細深入的研究了一下正規,寫上癮了,就當時能想到的都寫了):
    匹配圖象; 2 匹配email地址; 3 匹配匹配并提取url ; 4 匹配并提取http ;
    匹配日期 6 匹配電話; 7 匹配身份證 8 匹配郵編代碼
    不包括特殊字符的匹配 (字符串中不包括符號 數學次方號^ 單引號' 雙引號" 分號; 逗號, 帽號: 數學減號- 右尖括號> 左尖括號< 反斜杠\ 即空格,制表符,回車符等

    10 匹配非負整數(正整數 + 0) 11 匹配不包括零的非負整數(正整數 > 0)

    12 匹配正整數 13 匹配非正整數(負整數 + 0)

    14 匹配負整數; 15. 匹配整數 ;

    16 匹配非負浮點數(正浮點數 + 0) 17. 匹配正浮點數

    18 匹配非正浮點數(負浮點數 + 0) 19 匹配負浮點數;

    20 .匹配浮點數; 21. 匹配由26個英文字母組成的字符串;
    匹配由26個英文字母的大寫組成的字符串 23 匹配由26個英文字母的小寫組成的字符串

    24 匹配由數字和26個英文字母組成的字符串; 25 匹配由數字、26個英文字母或者下劃線組成的字符串;
    Java代碼
       1. java 代碼   
       
    2import java.util.*;  
       
    3import org.apache.oro.text.regex.*;  
       
    4.   
       
    5/** 
       6.  * 類簡介: 使用正則表達式驗證數據或提取數據,類中的方法全為靜態的 主要方法:1. isHardRegexpValidate(String source, 
       7.  * String regexp) 
       8.  *  
       9.  *  
      10.  * 區分大小寫敏感的正規表達式批配 2. isSoftRegexpValidate(String source, String regexp) 
      11.  * 不區分大小寫的正規表達式批配 3. getHardRegexpMatchResult(String source, String regexp) 
      12.  * 返回許要的批配結果集(大小寫敏感的正規表達式批配) 4. getSoftRegexpMatchResult(String source, String 
      13.  * regexp) 返回許要的批配結果集(不區分大小寫的正規表達式批配) 5 getHardRegexpArray(String source, String 
      14.  * regexp) 返回許要的批配結果集(大小寫敏感的正規表達式批配) 6. getSoftRegexpMatchResult(String source, 
      15.  * String regexp) 返回許要的批配結果集(不區分大小寫的正規表達式批配) 7. getBetweenSeparatorStr(final 
      16.  * String originStr,final char leftSeparator,final char rightSeparator) 
      17.  * 得到指定分隔符中間的字符串的集合 
      18.  *  
      19.  * @mail <A href="mailto:wuzhi2000@hotmail.com">wuzhi2000@hotmail.com</A> 
      20.  * 
    @author ygj 
      21.  *  
      22.  
    */  
      
    23public final class Regexp {  
      
    24.   /** 保放有四組對應分隔符 */  
      
    25.   static final Set SEPARATOR_SET = new TreeSet();  
      
    26.   {  
      
    27.     SEPARATOR_SET.add("(");  
      
    28.     SEPARATOR_SET.add(")");  
      
    29.     SEPARATOR_SET.add("[");  
      
    30.     SEPARATOR_SET.add("]");  
      
    31.     SEPARATOR_SET.add("{");  
      
    32.     SEPARATOR_SET.add("}");  
      
    33.     SEPARATOR_SET.add("<");  
      
    34.     SEPARATOR_SET.add(">");  
      
    35.   }  
      
    36.   /** 存放各種正規表達式(以key->value的形式) */  
      
    37.   public static HashMap regexpHash = new HashMap();  
      
    38.   /** 存放各種正規表達式(以key->value的形式) */  
      
    39.   public static List matchingResultList = new ArrayList();  
      
    40.   
      
    41.   private Regexp() {  
      
    42.   }  
      
    43.   
      
    44.   /** 
      45.    * 返回 Regexp 實例 
      46.    *  
      47.    * 
    @return 
      48.    
    */  
      
    49.   public static Regexp getInstance() {  
      
    50.     return new Regexp();  
      
    51.   }  
      
    52.   
      
    53.   /** 
      54.    * 匹配圖象 
      55.    *  
      56.    * 格式: /相對路徑/文件名.后綴 (后綴為gif,dmp,png) 
      57.    *  
      58.    * 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp 
      59.    *  
      60.    * 不匹配: c:/admins4512.gif 
      61.    *  
      62.    
    */  
      
    63.   public static final String icon_regexp = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg)$|^\\w{1,}\\.(gif|dmp|png|jpg)$";  
      
    64.   /** 
      65.    * 匹配email地址 
      66.    *  
      67.    * 格式: <A href="mailto:XXX@XXX.XXX.XX">XXX@XXX.XXX.XX</A> 
      68.    *  
      69.    * 匹配 : <A href="mailto:foo@bar.com">foo@bar.com</A> 或 < A href="mailto:foobar@foobar.com.au">foobar@foobar.com.au</A> 
      70.    *  
      71.    * 不匹配: foo@bar 或 $$$@bar.com 
      72.    *  
      73.    
    */  
      
    74.   public static final String email_regexp = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";  
      
    75.   /** 
      76.    * 匹配匹配并提取url 
      77.    *  
      78.    * 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX 
      79.    *  
      80.    * 匹配 : <A href="
    http://www.suncer.com" target=_blank>http://www.suncer.com</A> 或news://www 
      81.    *  
      82.    * 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)= 
      83.    * <A href="
    http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> matchResult.group(1) = 
      84.    * http matchResult.group(2) = <A href="www.suncer.com" target=_blank>www.suncer.com</A> matchResult.group(3) = :8080 
      85.    * matchResult.group(4) = /index.html?login=true 
      86.    *  
      87.    * 不匹配: c:\window 
      88.    *  
      89.    
    */  
      
    90.   public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";  
      
    91.   /** 
      92.    * 匹配并提取http 
      93.    *  
      94.    * 格式: <A href="
    http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX" target=_blank> http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX</A> 或 <A href="ftp: //XXX.XXX.XXX" target=_blank>ftp://XXX.XXX.XXX</A> 或 https: //XXX 
      95.    *  
      96.    * 匹配 : <A href="
    http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> 
      97.    *  
      98.    * 提取(MatchResult matchResult=matcher.getMatch()): matchResult.group(0)= 
      99.    * <A href="
    http://www.suncer.com:8080/index.html?login=true" target=_blank>http://www.suncer.com:8080/index.html?login=true</A> matchResult.group(1) = 
     100.    * http matchResult.group(2) = <A href="www.suncer.com" target=_blank>www.suncer.com</A> matchResult.group(3) = :8080 
     101.    * matchResult.group(4) = /index.html?login=true 
     102.    *  
     103.    * 不匹配: news://www 
     104.    *  
     105.    
    */  
     
    106.   public static final String http_regexp = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";  
     
    107.   /** 
     108.    * 匹配日期 
     109.    *  
     110.    * 格式(首位不為0): XXXX-XX-XX 或 XXXX XX XX 或 XXXX-X-X 
     111.    *  
     112.    * 范圍:1900--2099 
     113.    *  
     114.    * 匹配 : 2005-04-04 
     115.    *  
     116.    * 不匹配: 01-01-01 
     117.    *  
     118.    
    */  
     
    119.   public static final String date_regexp = "^((((19){1}|(20){1})d{2})|d{2})[-\\s]{1}[01]{1}d{1}[-\\s]{1}[0-3]{1}d{1}$";// 匹配日期  
     120.   /** 
     121.    * 匹配電話 
     122.    *  
     123.    * 格式為: 0XXX-XXXXXX(10-13位首位必須為0) 或0XXX XXXXXXX(10-13位首位必須為0) 或 
     124.    * (0XXX)XXXXXXXX(11-14位首位必須為0) 或 XXXXXXXX(6-8位首位不為0) 或 XXXXXXXXXXX(11位首位不為0) 
     125.    *  
     126.    * 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或 
     127.    * 010-12345678 或 12345678912 
     128.    *  
     129.    * 不匹配: 1111-134355 或 0123456789 
     130.    *  
     131.    
    */  
     
    132.   public static final String phone_regexp = "^(?:0[0-9]{2,3}[-\\s]{1}|\\(0[0-9]{2,4}\\))[0-9]{6,8}$|^[1-9]{1}[0-9]{5,7}$|^[1-9]{1}[0-9]{10}$";  
     
    133.   /** 
     134.    * 匹配身份證 
     135.    *  
     136.    * 格式為: XXXXXXXXXX(10位) 或 XXXXXXXXXXXXX(13位) 或 XXXXXXXXXXXXXXX(15位) 或 
     137.    * XXXXXXXXXXXXXXXXXX(18位) 
     138.    *  
     139.    * 匹配 : 0123456789123 
     140.    *  
     141.    * 不匹配: 0123456 
     142.    *  
     143.    
    */  
     
    144.   public static final String ID_card_regexp = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";  
     
    145.   /** 
     146.    * 匹配郵編代碼 
     147.    *  
     148.    * 格式為: XXXXXX(6位) 
     149.    *  
     150.    * 匹配 : 012345 
     151.    *  
     152.    * 不匹配: 0123456 
     153.    *  
     154.    
    */  
     
    155.   public static final String ZIP_regexp = "^[0-9]{6}$";// 匹配郵編代碼  
     156.   /** 
     157.    * 不包括特殊字符的匹配 (字符串中不包括符號 數學次方號^ 單引號' 雙引號" 分號; 逗號, 帽號: 數學減號- 右尖括號> 左尖括號< 反斜杠\ 
     158.    * 即空格,制表符,回車符等 ) 
     159.    *  
     160.    * 格式為: x 或 一個一上的字符 
     161.    *  
     162.    * 匹配 : 012345 
     163.    *  
     164.    * 不匹配: 0123456 
     165.    *  
     166.    
    */  
     
    167.   public static final String non_special_char_regexp = "^[^'\"\\;,:-<>\\s].+$";// 匹配郵編代碼  
     168.   /** 
     169.    * 匹配非負整數(正整數 + 0) 
     170.    
    */  
     
    171.   public static final String non_negative_integers_regexp = "^\\d+$";  
     
    172.   /** 
     173.    * 匹配不包括零的非負整數(正整數 > 0) 
     174.    
    */  
     
    175.   public static final String non_zero_negative_integers_regexp = "^[1-9]+\\d*$";  
     
    176.   /** 
     177.    *  
     178.    * 匹配正整數 
     179.    *  
     180.    
    */  
     
    181.   public static final String positive_integer_regexp = "^[0-9]*[1-9][0-9]*$";  
     
    182.   /** 
     183.    *  
     184.    * 匹配非正整數(負整數 + 0) 
     185.    *  
     186.    
    */  
     
    187.   public static final String non_positive_integers_regexp = "^((-\\d+)|(0+))$";  
     
    188.   /** 
     189.    *  
     190.    * 匹配負整數 
     191.    *  
     192.    
    */  
     
    193.   public static final String negative_integers_regexp = "^-[0-9]*[1-9][0-9]*$";  
     
    194.   /** 
     195.    *  
     196.    * 匹配整數 
     197.    *  
     198.    
    */  
     
    199.   public static final String integer_regexp = "^-?\\d+$";  
     
    200.   /** 
     201.    *  
     202.    * 匹配非負浮點數(正浮點數 + 0) 
     203.    *  
     204.    
    */  
     
    205.   public static final String non_negative_rational_numbers_regexp = "^\\d+(\\.\\d+)?$";  
     
    206.   /** 
     207.    *  
     208.    * 匹配正浮點數 
     209.    *  
     210.    
    */  
     
    211.   public static final String positive_rational_numbers_regexp = "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";  
     
    212.   /** 
     213.    *  
     214.    * 匹配非正浮點數(負浮點數 + 0) 
     215.    *  
     216.    
    */  
     
    217.   public static final String non_positive_rational_numbers_regexp = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";  
     
    218.   /** 
     219.    *  
     220.    * 匹配負浮點數 
     221.    *  
     222.    
    */  
     
    223.   public static final String negative_rational_numbers_regexp = "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$";  
     
    224.   /** 
     225.    *  
     226.    * 匹配浮點數 
     227.    *  
     228.    
    */  
     
    229.   public static final String rational_numbers_regexp = "^(-?\\d+)(\\.\\d+)?$";  
     
    230.   /** 
     231.    *  
     232.    * 匹配由26個英文字母組成的字符串 
     233.    *  
     234.    
    */  
     
    235.   public static final String letter_regexp = "^[A-Za-z]+$";  
     
    236.   /** 
     237.    *  
     238.    * 匹配由26個英文字母的大寫組成的字符串 
     239.    *  
     240.    
    */  
     
    241.   public static final String upward_letter_regexp = "^[A-Z]+$";  
     
    242.   /** 
     243.    *  
     244.    * 匹配由26個英文字母的小寫組成的字符串 
     245.    *  
     246.    
    */  
     
    247.   public static final String lower_letter_regexp = "^[a-z]+$";  
     
    248.   /** 
     249.    *  
     250.    * 匹配由數字和26個英文字母組成的字符串 
     251.    *  
     252.    
    */  
     
    253.   public static final String letter_number_regexp = "^[A-Za-z0-9]+$";  
     
    254.   /** 
     255.    *  
     256.    * 匹配由數字、26個英文字母或者下劃線組成的字符串 
     257.    *  
     258.    
    */  
     
    259.   public static final String letter_number_underline_regexp = "^\\w+$";  
     
    260.   
     
    261.   /** 
     262.    * 添加正規表達式 (以key->value的形式存儲) 
     263.    *  
     264.    * 
    @param regexpName 
     265.    *          該正規表達式名稱 ` 
     266.    * 
    @param regexp 
     267.    *          該正規表達式內容 
     268.    
    */  
     
    269.   public void putRegexpHash(String regexpName, String regexp) {  
     
    270.     regexpHash.put(regexpName, regexp);  
     
    271.   }  
     
    272.   
     
    273.   /** 
     274.    * 得到正規表達式內容 (通過key名提取出value[正規表達式內容]) 
     275.    *  
     276.    * 
    @param regexpName 
     277.    *          正規表達式名稱 
     278.    *  
     279.    * 
    @return 正規表達式內容 
     280.    
    */  
     
    281.   public String getRegexpHash(String regexpName) {  
     
    282.     if (regexpHash.get(regexpName) != null) {  
     
    283.       return ((String) regexpHash.get(regexpName));  
     
    284.     } else {  
     
    285.       System.out.println("在regexpHash中沒有此正規表達式");  
     
    286.       return "";  
     
    287.     }  
     
    288.   }  
     
    289.   
     
    290.   /** 
     291.    * 清除正規表達式存放單元 
     292.    
    */  
     
    293.   public void clearRegexpHash() {  
     
    294.     regexpHash.clear();  
     
    295.     return;  
     
    296.   }  
     
    297.   
     
    298.   /** 
     299.    * 大小寫敏感的正規表達式批配 
     300.    *  
     301.    * 
    @param source 
     302.    *          批配的源字符串 
     303.    *  
     304.    * 
    @param regexp 
     305.    *          批配的正規表達式 
     306.    *  
     307.    * 
    @return 如果源字符串符合要求返回真,否則返回假 如: 
     308.    *         Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真 
     309.    
    */  
     
    310.   public static boolean isHardRegexpValidate(String source, String regexp) {  
     
    311.     try {  
     
    312.       // 用于定義正規表達式對象模板類型  
     313.       PatternCompiler compiler = new Perl5Compiler();  
     
    314.       // 正規表達式比較批配對象  
     315.       PatternMatcher matcher = new Perl5Matcher();  
     
    316.       // 實例大小大小寫敏感的正規表達式模板  
     317.       Pattern hardPattern = compiler.compile(regexp);  
     
    318.       // 返回批配結果  
     319.       return matcher.contains(source, hardPattern);  
     
    320.     } catch (MalformedPatternException e) {  
     
    321.       e.printStackTrace();  
     
    322.     }  
     
    323.     return false;  
     
    324.   }  
     
    325. } 

    posted on 2009-08-08 09:01 石頭@ 閱讀(1266) 評論(0)  編輯  收藏 所屬分類: 基礎技術

    主站蜘蛛池模板: 亚洲第一永久在线观看| 91亚洲国产成人精品下载| 亚洲精品无码人妻无码| 拨牐拨牐x8免费| 国产 亚洲 中文在线 字幕| 97视频热人人精品免费| 亚洲中文字幕久久久一区| 国产精品69白浆在线观看免费| 亚洲日本国产精华液| 四虎永久在线观看免费网站网址 | 你懂的网址免费国产| 亚洲电影免费在线观看| 777爽死你无码免费看一二区| 亚洲欧洲日本天天堂在线观看| 91精品免费国产高清在线| 精品亚洲456在线播放| 国产成人无码免费视频97| 搜日本一区二区三区免费高清视频| 亚洲国产精品自在拍在线播放 | a在线观看免费视频| 老司机亚洲精品影院无码| 青青在线久青草免费观看| 亚洲大码熟女在线观看| 国产日产亚洲系列最新| 午夜不卡久久精品无码免费| 亚洲国产日韩在线成人蜜芽 | 国产免费MV大全视频网站| 亚洲av无码专区国产乱码在线观看 | 全部免费国产潢色一级| 狠狠躁狠狠爱免费视频无码| 久久久久亚洲Av无码专| 日日夜夜精品免费视频| 91精品全国免费观看青青| 91亚洲导航深夜福利| 免费人成视频在线观看视频| 久久精品免费视频观看| 亚洲精品无码久久久久秋霞| 亚洲熟女少妇一区二区| 欧美男同gv免费网站观看 | 无码精品一区二区三区免费视频| 在线观看亚洲AV每日更新无码|