空間站

          北極心空

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

          今天加班,一個同事讓我給他講解一下正規表達式的用法。

          猛然想起兩年寫了一個java的正規表達式的java工具類,分享一下,有用到的歡迎下載使用。

          如果你有常用的定義好的,且測試通過的正規表達式,歡迎跟貼,也讓我享用一下 .

          類中用到了 jakarta-oro-2.0.jar 包,請大家自己在 apache網站下下載

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

          這個工具類目前主要有25種正規表達式(有些不常用,但那時才仔細深入的研究了一下正規,寫上癮了,就當時能想到的都寫了):

           1.匹配圖象;                      2 匹配email地址;                    3 匹配匹配并提取url ;                         4 匹配并提取http ;

           5.匹配日期                       6 匹配電話;                               7 匹配身份證                                       8 匹配郵編代碼

          9. 不包括特殊字符的匹配 (字符串中不包括符號 數學次方號^ 單引號' 雙引號" 分號; 逗號, 帽號: 數學減號- 右尖括號> 左尖括號<  反斜杠\ 即空格,制表符,回車符等 

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

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

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

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

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

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

          22. 匹配由26個英文字母的大寫組成的字符串                   23 匹配由26個英文字母的小寫組成的字符串 

          24 匹配由數字和26個英文字母組成的字符串;                   25  匹配由數字、26個英文字母或者下劃線組成的字符串;

          java 代碼
          1.     
          2. package com.ygj.util;   
          3.   
          4. import java.util.*;   
          5.   
          6. import org.apache.oro.text.regex.*;   
          7.  
          8.   
          9. /**  
          10.  * 類簡介: 使用正則表達式驗證數據或提取數據,類中的方法全為靜態的
             * 主要方法:1. isHardRegexpValidate(String source, String regexp)  
          11.               區分大小寫敏感的正規表達式批配   
          12.  *          2. isSoftRegexpValidate(String source, String regexp)  
          13.  *             不區分大小寫的正規表達式批配  
          14.  *          3. getHardRegexpMatchResult(String source, String regexp)  
          15.  *             返回許要的批配結果集(大小寫敏感的正規表達式批配)  
          16.  *          4. getSoftRegexpMatchResult(String source, String regexp)  
          17.  *             返回許要的批配結果集(不區分大小寫的正規表達式批配)  
          18.  *          5  getHardRegexpArray(String source, String regexp)  
          19.  *             返回許要的批配結果集(大小寫敏感的正規表達式批配)  
          20.  *          6. getSoftRegexpMatchResult(String source, String regexp)  
          21.  *             返回許要的批配結果集(不區分大小寫的正規表達式批配)  
          22.  *          7.  getBetweenSeparatorStr(final String originStr,final char leftSeparator,final char rightSeparator)  
          23.  *             得到指定分隔符中間的字符串的集合  
          24.  *  
          25.  * @mail wuzhi2000@hotmail.com  
          26.  * @author ygj  
          27.  *  
          28.  */  
          29. public final class Regexp   
          30. {   
          31.   
          32.     /**  保放有四組對應分隔符 */  
          33.     static final  Set SEPARATOR_SET=new TreeSet();   
          34.     {   
          35.                SEPARATOR_SET.add("(");   
          36.                SEPARATOR_SET.add(")");   
          37.                SEPARATOR_SET.add("[");   
          38.                SEPARATOR_SET.add("]");   
          39.                SEPARATOR_SET.add("{");   
          40.                SEPARATOR_SET.add("}");   
          41.                SEPARATOR_SET.add("<");   
          42.                SEPARATOR_SET.add(">");   
          43.     }   
          44.   
          45.   
          46.     /** 存放各種正規表達式(以key->value的形式) */  
          47.      public static HashMap regexpHash = new HashMap();   
          48.   
          49.     /** 存放各種正規表達式(以key->value的形式) */  
          50.     public static  List matchingResultList = new ArrayList();   
          51.   
          52.    private       Regexp()   
          53.     {   
          54.   
          55.     }   
          56.     /**  
          57.      * 返回 Regexp 實例  
          58.      * @return  
          59.      */  
          60.     public static Regexp getInstance()   
          61.     {   
          62.         return new Regexp();   
          63.     }   
          64.   
          65.     /**  
          66.      * 匹配圖象 
             
          67.      *  
          68.      * 格式: /相對路徑/文件名.后綴 (后綴為gif,dmp,png)  
          69.      *  
          70.      * 匹配 : /forum/head_icon/admini2005111_ff.gif 或 admini2005111.dmp
             
          71.      *  
          72.      * 不匹配: c:/admins4512.gif  
          73.      *  
          74.      */  
          75.     public static final String icon_regexp = "^(/{0,1}\\w){1,}\\.(gif|dmp|png|jpg)$|^\\w{1,}\\.(gif|dmp|png|jpg)$";   
          76.   
          77.     /**  
          78.      * 匹配email地址 
             
          79.      *  
          80.      * 格式: XXX@XXX.XXX.XX  
          81.      *  
          82.      * 匹配 : foo@bar.com 或 foobar@foobar.com.au 
             
          83.      *  
          84.      * 不匹配: foo@bar 或 $$$@bar.com  
          85.      *  
          86.      */  
          87.     public static final String email_regexp = "(?:\\w[-._\\w]*\\w@\\w[-._\\w]*\\w\\.\\w{2,3}$)";   
          88.   
          89.     /**  
          90.      * 匹配匹配并提取url 
             
          91.      *  
          92.      * 格式: XXXX://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX  
          93.      *  
          94.      * 匹配 : http://www.suncer.com 或news://www
             
          95.      *  
          96.      * 提取(MatchResult matchResult=matcher.getMatch()):  
          97.      *              matchResult.group(0)= http://www.suncer.com:8080/index.html?login=true  
          98.      *              matchResult.group(1) = http  
          99.      *              matchResult.group(2) = www.suncer.com  
          100.      *              matchResult.group(3) = :8080  
          101.      *              matchResult.group(4) = /index.html?login=true  
          102.      *  
          103.      * 不匹配: c:\window  
          104.      *  
          105.      */  
          106.     public static final String url_regexp = "(\\w+)://([^/:]+)(:\\d*)?([^#\\s]*)";   
          107.   
          108.     /**  
          109.      * 匹配并提取http 
             
          110.      *  
          111.      * 格式: http://XXX.XXX.XXX.XX/XXX.XXX?XXX=XXX 或 ftp://XXX.XXX.XXX 或 https://XXX  
          112.      *  
          113.      * 匹配 : http://www.suncer.com:8080/index.html?login=true
             
          114.      *  
          115.      * 提取(MatchResult matchResult=matcher.getMatch()):  
          116.      *              matchResult.group(0)= http://www.suncer.com:8080/index.html?login=true  
          117.      *              matchResult.group(1) = http  
          118.      *              matchResult.group(2) = www.suncer.com  
          119.      *              matchResult.group(3) = :8080  
          120.      *              matchResult.group(4) = /index.html?login=true  
          121.      *  
          122.      * 不匹配: news://www  
          123.      *  
          124.      */  
          125.     public static final String http_regexp = "(http|https|ftp)://([^/:]+)(:\\d*)?([^#\\s]*)";   
          126.   
          127.     /**  
          128.      * 匹配日期 
             
          129.      *  
          130.      * 格式(首位不為0): XXXX-XX-XX 或 XXXX XX XX 或 XXXX-X-X 
             
          131.      *  
          132.      * 范圍:1900--2099 
             
          133.      *  
          134.      * 匹配 : 2005-04-04 
             
          135.      *  
          136.      * 不匹配: 01-01-01  
          137.      *  
          138.      */  
          139.     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}$";// 匹配日期   
          140.   
          141.     /**  
          142.      * 匹配電話 
             
          143.      *  
          144.      * 格式為: 0XXX-XXXXXX(10-13位首位必須為0) 或0XXX XXXXXXX(10-13位首位必須為0) 或 
             
          145.      * (0XXX)XXXXXXXX(11-14位首位必須為0) 或 XXXXXXXX(6-8位首位不為0) 或  
          146.      * XXXXXXXXXXX(11位首位不為0) 
             
          147.      *  
          148.      * 匹配 : 0371-123456 或 (0371)1234567 或 (0371)12345678 或 010-123456 或  
          149.      * 010-12345678 或 12345678912 
             
          150.      *  
          151.      * 不匹配: 1111-134355 或 0123456789  
          152.      *  
          153.      */  
          154.     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}$";   
          155.   
          156.     /**  
          157.      * 匹配身份證 
             
          158.      *  
          159.      * 格式為: XXXXXXXXXX(10位) 或 XXXXXXXXXXXXX(13位) 或 XXXXXXXXXXXXXXX(15位) 或  
          160.      * XXXXXXXXXXXXXXXXXX(18位) 
             
          161.      *  
          162.      * 匹配 : 0123456789123 
             
          163.      *  
          164.      * 不匹配: 0123456  
          165.      *  
          166.      */  
          167.     public static final String ID_card_regexp = "^\\d{10}|\\d{13}|\\d{15}|\\d{18}$";   
          168.   
          169.     /**  
          170.      * 匹配郵編代碼 
             
          171.      *  
          172.      * 格式為: XXXXXX(6位) 
             
          173.      *  
          174.      * 匹配 : 012345 
             
          175.      *  
          176.      * 不匹配: 0123456  
          177.      *  
          178.      */  
          179.     public static final String ZIP_regexp = "^[0-9]{6}$";// 匹配郵編代碼   
          180.   
          181.   
          182.     /**  
          183.      * 不包括特殊字符的匹配 (字符串中不包括符號 數學次方號^ 單引號' 雙引號" 分號; 逗號, 帽號: 數學減號- 右尖括號> 左尖括號<  反斜杠\ 即空格,制表符,回車符等 )
             
          184.      *  
          185.      * 格式為: x 或 一個一上的字符 
             
          186.      *  
          187.      * 匹配 : 012345 
             
          188.      *  
          189.      * 不匹配: 0123456  
          190.      *  
          191.      */  
          192.     public static final String non_special_char_regexp = "^[^'\"\\;,:-<>\\s].+$";// 匹配郵編代碼   
          193.   
          194.   
          195.     /**  
          196.      * 匹配非負整數(正整數 + 0)  
          197.      */  
          198.     public static final String non_negative_integers_regexp = "^\\d+$";   
          199.   
          200.     /**  
          201.      * 匹配不包括零的非負整數(正整數 > 0)  
          202.      */  
          203.     public static final String non_zero_negative_integers_regexp = "^[1-9]+\\d*$";   
          204.   
          205.     /**  
          206.      *  
          207.      * 匹配正整數  
          208.      *  
          209.      */  
          210.     public static final String positive_integer_regexp = "^[0-9]*[1-9][0-9]*$";   
          211.   
          212.     /**  
          213.      *  
          214.      * 匹配非正整數(負整數 + 0)  
          215.      *  
          216.      */  
          217.     public static final String non_positive_integers_regexp = "^((-\\d+)|(0+))$";   
          218.   
          219.     /**  
          220.      *  
          221.      * 匹配負整數  
          222.      *  
          223.      */  
          224.     public static final String negative_integers_regexp = "^-[0-9]*[1-9][0-9]*$";   
          225.   
          226.     /**  
          227.      *  
          228.      * 匹配整數  
          229.      *  
          230.      */  
          231.     public static final String integer_regexp = "^-?\\d+$";   
          232.   
          233.     /**  
          234.      *  
          235.      * 匹配非負浮點數(正浮點數 + 0)  
          236.      *  
          237.      */  
          238.     public static final String non_negative_rational_numbers_regexp = "^\\d+(\\.\\d+)?$";   
          239.   
          240.     /**  
          241.      *  
          242.      * 匹配正浮點數  
          243.      *  
          244.      */  
          245.     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]*))$";   
          246.   
          247.     /**  
          248.      *  
          249.      * 匹配非正浮點數(負浮點數 + 0)  
          250.      *  
          251.      */  
          252.     public static final String non_positive_rational_numbers_regexp = "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$";   
          253.   
          254.     /**  
          255.      *  
          256.      * 匹配負浮點數  
          257.      *  
          258.      */  
          259.     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]*)))$";   
          260.   
          261.     /**  
          262.      *  
          263.      * 匹配浮點數  
          264.      *  
          265.      */  
          266.     public static final String rational_numbers_regexp = "^(-?\\d+)(\\.\\d+)?$";   
          267.   
          268.     /**  
          269.      *  
          270.      * 匹配由26個英文字母組成的字符串  
          271.      *  
          272.      */  
          273.     public static final String letter_regexp = "^[A-Za-z]+$";   
          274.   
          275.     /**  
          276.      *  
          277.      * 匹配由26個英文字母的大寫組成的字符串  
          278.      *  
          279.      */  
          280.     public static final String upward_letter_regexp = "^[A-Z]+$";   
          281.   
          282.     /**  
          283.      *  
          284.      * 匹配由26個英文字母的小寫組成的字符串  
          285.      *  
          286.      */  
          287.     public static final String lower_letter_regexp = "^[a-z]+$";   
          288.   
          289.     /**  
          290.      *  
          291.      * 匹配由數字和26個英文字母組成的字符串  
          292.      *  
          293.      */  
          294.     public static final String letter_number_regexp = "^[A-Za-z0-9]+$";   
          295.   
          296.     /**  
          297.      *  
          298.      * 匹配由數字、26個英文字母或者下劃線組成的字符串  
          299.      *  
          300.      */  
          301.     public static final String letter_number_underline_regexp = "^\\w+$";   
          302.   
          303.     /**  
          304.      * 添加正規表達式 (以key->value的形式存儲)  
          305.      *  
          306.      * @param regexpName  
          307.      *            該正規表達式名稱 `  
          308.      * @param regexp  
          309.      *            該正規表達式內容  
          310.      */  
          311.     public void putRegexpHash(String regexpName, String regexp)   
          312.     {   
          313.         regexpHash.put(regexpName, regexp);   
          314.     }   
          315.   
          316.     /**  
          317.      * 得到正規表達式內容 (通過key名提取出value[正規表達式內容])  
          318.      *  
          319.      * @param regexpName  
          320.      *            正規表達式名稱  
          321.      *  
          322.      * @return 正規表達式內容  
          323.      */  
          324.     public String getRegexpHash(String regexpName)   
          325.     {   
          326.         if (regexpHash.get(regexpName) != null)   
          327.         {   
          328.             return ((String) regexpHash.get(regexpName));   
          329.         }   
          330.         else  
          331.         {   
          332.             System.out.println("在regexpHash中沒有此正規表達式");   
          333.             return "";   
          334.         }   
          335.     }   
          336.   
          337.     /**  
          338.      * 清除正規表達式存放單元  
          339.      */  
          340.     public void clearRegexpHash()   
          341.     {   
          342.         regexpHash.clear();   
          343.         return;   
          344.     }   
          345.   
          346.     /**  
          347.      * 大小寫敏感的正規表達式批配  
          348.      *  
          349.      * @param source  
          350.      *            批配的源字符串  
          351.      *  
          352.      * @param regexp  
          353.      *            批配的正規表達式  
          354.      *  
          355.      * @return 如果源字符串符合要求返回真,否則返回假 如:  Regexp.isHardRegexpValidate("ygj@suncer.com.cn",email_regexp) 返回真  
          356.      */  
          357.     public static boolean isHardRegexpValidate(String source, String regexp)   
          358.     {   
          359.   
          360.         try  
          361.         {   
          362.             // 用于定義正規表達式對象模板類型   
          363.             PatternCompiler compiler = new Perl5Compiler();   
          364.   
          365.             // 正規表達式比較批配對象   
          366.             PatternMatcher matcher = new Perl5Matcher();   
          367.   
          368.             // 實例大小大小寫敏感的正規表達式模板   
          369.             Pattern hardPattern = compiler.compile(regexp);   
          370.   
          371.             // 返回批配結果   
          372.             return matcher.contains(source, hardPattern);   
          373.   
          374.         }   
          375.         catch (MalformedPatternException e)   
          376.         {   
          377.             e.printStackTrace();   
          378.   
          379.         }   
          380.         return false;   
          381.     }   
          382.   
          • Regexp.zip (9.7 KB)
          • 描述: java源碼
          • 下載次數: 962
          http://www.javaeye.com/topic/67398
          posted on 2008-11-06 12:57 蘆葦 閱讀(669) 評論(0)  編輯  收藏 所屬分類: JAVA其他
          主站蜘蛛池模板: 宁乡县| 商洛市| 灵台县| 天柱县| 扎赉特旗| 齐河县| 海盐县| 收藏| 日土县| 开鲁县| 陆川县| 蛟河市| 商城县| 泗水县| 肇庆市| 商水县| 阳东县| 大新县| 栖霞市| 阳泉市| 鄂托克前旗| 庄河市| 明星| 二手房| 澄迈县| 鄄城县| 屯留县| 渝北区| 河南省| 巴里| 木里| 西华县| 永宁县| 安徽省| 那坡县| 景泰县| 邯郸市| 广平县| 汉沽区| 铁岭市| 肇源县|