分享一个java正则验证类

 

原文章:http://blog.csdn.net/jarvis_java/article/details/5949096

分享一个java正则验证类

分类: Java   140人阅读   评论(0)   收藏   举报

   网上正则验证的很多,但是没找到一个比较集中的类,这个是我工作中收集的,现在共享出来。方便大家使用,

这个类可以直接运用到项目中。

  1. package com.tool.util;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5. /** 
  6.  *  
  7.  * @author Jarvis 
  8.  * 90%的验证都调用了Regular方法 但是本类也可删除大部分方法 涉及到正则的判断都直接穿参数和正则表达式 
  9.  * 但是为了方便业务类调用和有更直观的含义 建议不要这么做 
  10.  * Pattern的matcher已经被同步synchronized 所以 此类的任何使用正则验证的方法都不需要同步 
  11.  * 
  12.  */  
  13. public class validate {  
  14.     public validate() {  
  15.           
  16.     }  
  17.     //------------------常量定义  
  18.     /** 
  19.      * Email正则表达式=^/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$  
  20.      */  
  21.     public static final String EMAIL = "^//w+([-+.]//w+)*@//w+([-.]//w+)*//.//w+([-.]//w+)*$";  
  22.       
  23.     /** 
  24.      * 电话号码正则表达式= (^(/d{2,4}[-_-—]?)?/d{3,8}([-_-—]?/d{3,8})?([-_-—]?/d{1,7})?$)|(^0?1[35]/d{9}$)  
  25.      */  
  26.     public static final String PHONE = "(^(//d{2,4}[-_-—]?)?//d{3,8}([-_-—]?//d{3,8})?([-_-—]?//d{1,7})?$)|(^0?1[35]//d{9}$)" ;  
  27.     /** 
  28.      * 手机号码正则表达式=^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])/d{8}$ 
  29.      */  
  30.     public static final String MOBILE ="^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])//d{8}$";  
  31.   
  32.     /** 
  33.      * IP地址正则表达式 ((?:(?:25[0-5]|2[0-4]//d|[01]?//d?//d)//.){3}(?:25[0-5]|2[0-4]//d|[01]?//d?//d)) 
  34.      */  
  35.     public static final String IPADDRESS = "((?:(?:25[0-5]|2[0-4]//d|[01]?//d?//d)//.){3}(?:25[0-5]|2[0-4]//d|[01]?//d?//d))";  
  36.   
  37.     /** 
  38.      * Integer正则表达式 ^-?(([1-9]/d*$)|0) 
  39.      */  
  40.     public static final String  INTEGER = "^-?(([1-9]//d*$)|0)";  
  41.     /** 
  42.      * 正整数正则表达式 >=0 ^[1-9]/d*|0$ 
  43.      */  
  44.     public static final String  INTEGER_NEGATIVE = "^[1-9]//d*|0$";  
  45.     /** 
  46.      * 负整数正则表达式 <=0 ^-[1-9]/d*|0$ 
  47.      */  
  48.     public static final String  INTEGER_POSITIVE = "^-[1-9]//d*|0$";      
  49.     /** 
  50.      * Double正则表达式 ^-?([1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0)$  
  51.      */  
  52.     public static final String  DOUBLE ="^-?([1-9]//d*//.//d*|0//.//d*[1-9]//d*|0?//.0+|0)$";  
  53.     /** 
  54.      * 正Double正则表达式 >=0  ^[1-9]/d*/./d*|0/./d*[1-9]/d*|0?/.0+|0$   
  55.      */  
  56.     public static final String  DOUBLE_NEGATIVE ="^[1-9]//d*//.//d*|0//.//d*[1-9]//d*|0?//.0+|0$";  
  57.     /** 
  58.      * 负Double正则表达式 <= 0  ^(-([1-9]/d*/./d*|0/./d*[1-9]/d*))|0?/.0+|0$  
  59.      */  
  60.     public static final String  DOUBLE_POSITIVE ="^(-([1-9]//d*//.//d*|0//.//d*[1-9]//d*))|0?//.0+|0$";   
  61.     /** 
  62.      * 年龄正则表达式 ^(?:[1-9][0-9]?|1[01][0-9]|120)$ 匹配0-120岁 
  63.      */  
  64.     public static final String  AGE="^(?:[1-9][0-9]?|1[01][0-9]|120)$";  
  65.     /** 
  66.      * 邮编正则表达式  [1-9]/d{5}(?!/d) 国内6位邮编 
  67.      */  
  68.     public static final String  CODE="[1-9]//d{5}(?!//d)";    
  69.     /** 
  70.      * 匹配由数字、26个英文字母或者下划线组成的字符串 ^/w+$ 
  71.      */  
  72.     public static final String STR_ENG_NUM_="^//w+$";  
  73.     /** 
  74.      * 匹配由数字和26个英文字母组成的字符串 ^[A-Za-z0-9]+$  
  75.      */  
  76.     public static final String STR_ENG_NUM="^//w+$";  
  77.     /** 
  78.      * 匹配由26个英文字母组成的字符串  ^[A-Za-z]+$ 
  79.      */  
  80.     public static final String STR_ENG="^[A-Za-z]+$";  
  81.     /** 
  82.      * 匹配中文字符 ^[/u0391-/uFFE5]+$ 
  83.      */  
  84.     public static final String STR_CHINA="^[/u0391-/uFFE5]+$";    
  85.     /** 
  86.      * 过滤特殊字符串正则 
  87.      * regEx="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";  
  88.      */  
  89.     public static final String STR_SPECIAL="[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";  
  90.     /** 
  91.      *只能输英文 数字 中文 ^[a-zA-Z0-9/u4e00-/u9fa5]+$ 
  92.      */  
  93.     public static final String STR_ENG_CHA_NUM="^[a-zA-Z0-9/u4e00-/u9fa5]+$";  
  94.     /** 
  95.      *   
  96.      */  
  97.     /*** 
  98.      * 日期正则 支持: 
  99.      *  YYYY-MM-DD  
  100.      *  YYYY/MM/DD  
  101.      *  YYYY_MM_DD  
  102.      *  YYYYMMDD 
  103.      *  YYYY.MM.DD的形式 
  104.      */  
  105.     public static final String DATE_ALL="((^((1[8-9]//d{2})|([2-9]//d{3}))([-/._]?)(10|12|0?[13578])([-/._]?)(3[01]|[12][0-9]|0?[1-9])$)" +  
  106.             "|(^((1[8-9]//d{2})|([2-9]//d{3}))([-/._]?)(11|0?[469])([-/._]?)(30|[12][0-9]|0?[1-9])$)" +  
  107.             "|(^((1[8-9]//d{2})|([2-9]//d{3}))([-/._]?)(0?2)([-/._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-/._]?)(0?2)([-/._]?)(29)$)|(^([3579][26]00)" +  
  108.             "([-/._]?)(0?2)([-/._]?)(29)$)" +  
  109.             "|(^([1][89][0][48])([-/._]?)(0?2)([-/._]?)(29)$)|(^([2-9][0-9][0][48])([-/._]?)" +  
  110.             "(0?2)([-/._]?)(29)$)" +  
  111.             "|(^([1][89][2468][048])([-/._]?)(0?2)([-/._]?)(29)$)|(^([2-9][0-9][2468][048])([-/._]?)(0?2)" +  
  112.             "([-/._]?)(29)$)|(^([1][89][13579][26])([-/._]?)(0?2)([-/._]?)(29)$)|" +  
  113.             "(^([2-9][0-9][13579][26])([-/._]?)(0?2)([-/._]?)(29)$))";  
  114.   
  115.     /** 
  116.      * URL正则表达式 
  117.       * 匹配 http www ftp 
  118.      */  
  119.     public static final String URL = "^(http|www|ftp|)?(://)?(//w+(-//w+)*)(//.(//w+(-//w+)*))*((://d+)?)(/(//w+(-//w+)*))*(//.?(//w)*)(//?)?" +  
  120.                                     "(((//w*%)*(//w*//?)*(//w*:)*(//w*//+)*(//w*//.)*(//w*&)*(//w*-)*(//w*=)*(//w*%)*(//w*//?)*" +  
  121.                                     "(//w*:)*(//w*//+)*(//w*//.)*" +  
  122.                                     "(//w*&)*(//w*-)*(//w*=)*)*(//w*)*)$";   
  123.   
  124.     /** 
  125.      * 身份证正则表达式 
  126.      */  
  127.     public static final String IDCARD="((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +  
  128.                                         "(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +  
  129.                                         "[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";  
  130.        /** 
  131.         * 1.匹配科学计数 e或者E必须出现有且只有一次 不含Dd 
  132.         * 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$ 
  133.         */  
  134.        public final static String SCIENTIFIC_A ="^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))$";  
  135.        /** 
  136.         * 2.匹配科学计数 e或者E必须出现有且只有一次 结尾包含Dd 
  137.         * 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dD]?$ 
  138.         */  
  139.        public final static String SCIENTIFIC_B ="^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))[dD]?$";  
  140.        /** 
  141.          * 3.匹配科学计数 是否含有E或者e都通过 结尾含有Dd的也通过(针对Double类型) 
  142.          * 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dD]?$ 
  143.          */  
  144.        public final static String SCIENTIFIC_C ="^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?[dD]?$";  
  145.        /** 
  146.          * 4.匹配科学计数 是否含有E或者e都通过 结尾不含Dd 
  147.          * 正则 ^[-+]?(/d+(/./d*)?|/./d+)([eE]([-+]?([012]?/d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$ 
  148.         */  
  149.        public final static String SCIENTIFIC_D ="^[-+]?(//d+(//.//d*)?|//.//d+)([eE]([-+]?([012]?//d{1,2}|30[0-7])|-3([01]?[4-9]|[012]?[0-3])))?$";  
  150.       
  151. ------------------验证方法       
  152.     /** 
  153.      * 判断字段是否为空 符合返回ture 
  154.      * @param str 
  155.      * @return boolean 
  156.      */  
  157.     public static synchronized boolean StrisNull(String str) {  
  158.         return null == str || str.trim().length() <= 0 ? true : false ;  
  159.     }  
  160.     /** 
  161.      * 判断字段是非空 符合返回ture 
  162.      * @param str 
  163.      * @return boolean 
  164.      */  
  165.     public static  boolean StrNotNull(String str) {  
  166.         return !StrisNull(str) ;  
  167.     }  
  168.     /** 
  169.      * 字符串null转空 
  170.      * @param str 
  171.      * @return boolean 
  172.      */  
  173.     public static  String nulltoStr(String str) {  
  174.         return StrisNull(str)?"":str;  
  175.     }     
  176.     /** 
  177.      * 字符串null赋值默认值  
  178.      * @param str    目标字符串 
  179.      * @param defaut 默认值 
  180.      * @return String 
  181.      */  
  182.     public static  String nulltoStr(String str,String defaut) {  
  183.         return StrisNull(str)?defaut:str;  
  184.     }  
  185.     /** 
  186.      * 判断字段是否为Email 符合返回ture 
  187.      * @param str 
  188.      * @return boolean 
  189.      */  
  190.     public static  boolean isEmail(String str) {  
  191.         return Regular(str,EMAIL);  
  192.     }  
  193.     /** 
  194.      * 判断是否为电话号码 符合返回ture 
  195.      * @param str 
  196.      * @return boolean 
  197.      */  
  198.     public static  boolean isPhone(String str) {  
  199.         return Regular(str,PHONE);  
  200.     }  
  201.     /** 
  202.      * 判断是否为手机号码 符合返回ture 
  203.      * @param str 
  204.      * @return boolean 
  205.      */  
  206.     public static  boolean isMobile(String str) {  
  207.         return Regular(str,MOBILE);  
  208.     }  
  209.     /** 
  210.      * 判断是否为Url 符合返回ture 
  211.      * @param str 
  212.      * @return boolean 
  213.      */  
  214.     public static  boolean isUrl(String str) {  
  215.         return Regular(str,URL);  
  216.     }     
  217.     /** 
  218.      * 判断是否为IP地址 符合返回ture 
  219.      * @param str 
  220.      * @return boolean 
  221.      */  
  222.     public static  boolean isIpaddress(String str) {  
  223.         return Regular(str,IPADDRESS);  
  224.     }     
  225.     /**  
  226.      * 判断字段是否为数字 正负整数 正负浮点数 符合返回ture 
  227.      * @param str 
  228.      * @return boolean 
  229.      */  
  230.     public static  boolean isNumber(String str) {  
  231.         return Regular(str,DOUBLE);  
  232.     }  
  233.     /** 
  234.      * 判断字段是否为INTEGER  符合返回ture 
  235.      * @param str 
  236.      * @return boolean 
  237.      */  
  238.     public static  boolean isInteger(String str) {  
  239.         return Regular(str,INTEGER);  
  240.     }  
  241.     /** 
  242.      * 判断字段是否为正整数正则表达式 >=0 符合返回ture 
  243.      * @param str 
  244.      * @return boolean 
  245.      */  
  246.     public static  boolean isINTEGER_NEGATIVE(String str) {  
  247.         return Regular(str,INTEGER_NEGATIVE);  
  248.     }  
  249.     /** 
  250.      * 判断字段是否为负整数正则表达式 <=0 符合返回ture 
  251.      * @param str 
  252.      * @return boolean 
  253.      */  
  254.     public static  boolean isINTEGER_POSITIVE(String str) {  
  255.         return Regular(str,INTEGER_POSITIVE);  
  256.     }     
  257.     /** 
  258.      * 判断字段是否为DOUBLE 符合返回ture 
  259.      * @param str 
  260.      * @return boolean 
  261.      */  
  262.     public static  boolean isDouble(String str) {  
  263.         return Regular(str,DOUBLE);  
  264.     }  
  265.     /**  
  266.      * 判断字段是否为正浮点数正则表达式 >=0 符合返回ture 
  267.      * @param str 
  268.      * @return boolean 
  269.      */  
  270.     public static  boolean isDOUBLE_NEGATIVE(String str) {  
  271.         return Regular(str,DOUBLE_NEGATIVE);  
  272.     }  
  273.     /** 
  274.      * 判断字段是否为负浮点数正则表达式 <=0 符合返回ture 
  275.      * @param str 
  276.      * @return boolean 
  277.      */  
  278.     public static  boolean isDOUBLE_POSITIVE(String str) {  
  279.         return Regular(str,DOUBLE_POSITIVE);  
  280.     }     
  281.     /** 
  282.      * 判断字段是否为日期 符合返回ture 
  283.      * @param str 
  284.      * @return boolean 
  285.      */  
  286.     public static  boolean isDate(String str) {  
  287.         return Regular(str,DATE_ALL);  
  288.     }     
  289.     /** 
  290.      * 判断字段是否为年龄 符合返回ture 
  291.      * @param str 
  292.      * @return boolean 
  293.      */  
  294.     public static  boolean isAge(String str) {  
  295.         return Regular(str,AGE) ;  
  296.     }  
  297.     /** 
  298.      * 判断字段是否超长 
  299.      * 字串为空返回fasle, 超过长度{leng}返回ture 反之返回false 
  300.      * @param str 
  301.      * @param leng 
  302.      * @return boolean 
  303.      */  
  304.     public static  boolean isLengOut(String str,int leng) {       
  305.         return StrisNull(str)?false:str.trim().length() > leng ;  
  306.     }  
  307.     /** 
  308.      * 判断字段是否为身份证 符合返回ture 
  309.      * @param str 
  310.      * @return boolean 
  311.      */  
  312.     public static  boolean isIdCard(String str) {  
  313.         if(StrisNull(str)) return false ;  
  314.         if(str.trim().length() == 15 || str.trim().length() == 18) {  
  315.                 return Regular(str,IDCARD);  
  316.         }else {  
  317.             return false ;  
  318.         }  
  319.           
  320.     }  
  321.     /** 
  322.      * 判断字段是否为邮编 符合返回ture 
  323.      * @param str 
  324.      * @return boolean 
  325.      */  
  326.     public static  boolean isCode(String str) {  
  327.         return Regular(str,CODE) ;  
  328.     }  
  329.     /** 
  330.      * 判断字符串是不是全部是汉字 
  331.      * @param str 
  332.      * @return boolean 
  333.      */  
  334.     public static boolean isChina(String str) {  
  335.         return Regular(str,STR_CHINA) ;  
  336.     }  
  337.     /** 
  338.      * 判断字符串是不是全部是英文字母 
  339.      * @param str 
  340.      * @return boolean 
  341.      */  
  342.     public static boolean isEnglish(String str) {  
  343.         return Regular(str,STR_ENG) ;  
  344.     }  
  345.     /** 
  346.      * 判断字符串是不是全部是英文字母+数字 
  347.      * @param str 
  348.      * @return boolean 
  349.      */  
  350.     public static boolean isENG_NUM(String str) {  
  351.         return Regular(str,STR_ENG_NUM) ;  
  352.     }  
  353.     /** 
  354.      * 判断字符串是不是全部是英文字母+数字+下划线 
  355.      * @param str 
  356.      * @return boolean 
  357.      */  
  358.     public static boolean isENG_NUM_(String str) {  
  359.         return Regular(str,STR_ENG_NUM_) ;  
  360.     }  
  361.     /** 
  362.      * 过滤特殊字符串 返回过滤后的字符串 
  363.      * @param str 
  364.      * @return boolean 
  365.      */  
  366.     public static  String filterStr(String str) {  
  367.         Pattern p = Pattern.compile(STR_SPECIAL);  
  368.         Matcher m = p.matcher(str);  
  369.         return   m.replaceAll("").trim();  
  370.     }     
  371.     /** 
  372.      * 匹配是否符合正则表达式pattern 匹配返回true 
  373.      * @param str 匹配的字符串 
  374.      * @param pattern 匹配模式 
  375.      * @return boolean 
  376.      */  
  377.     private static  boolean Regular(String str,String pattern){  
  378.         System.out.println("pattern="+pattern);  
  379.         if(null == str || str.trim().length()<=0)  
  380.             return false;           
  381.         Pattern p = Pattern.compile(pattern);  
  382.         Matcher m = p.matcher(str);  
  383.         return m.matches();  
  384.     }  
  385.     /** 
  386.      * 判断是不是科学计数法 如果是 返回true 
  387.      * 匹配科学计数 e或者E必须出现有且只有一次 结尾不含D 
  388.      * 匹配模式可参考本类定义的 SCIENTIFIC_A,SCIENTIFIC_B,SCIENTIFIC_C,SCIENTIFIC_D 
  389.      * 若判断为其他模式可调用 Regular(String str,String pattern)方法 
  390.      * @param str 科学计数字符串 
  391.      * @return boolean 
  392.      */  
  393.     public static  boolean isScientific(String str){  
  394.         if(StrisNull(str))  
  395.             return false;   
  396.         return Regular(str,validate.SCIENTIFIC_A);  
  397.     }     
  398.     public static void main(String[] args) {  
  399.         // TODO Auto-generated method stub  
  400.         }  
  401.   
  402. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值