java 正则验证

 
  1. package com.ks.tools;   
  2.   
  3. import java.util.regex.Matcher;   
  4. import java.util.regex.Pattern;   
  5.   
  6. /**  
  7.  * @author 李英夫  
  8.  * @ClassName KSValidate  
  9.  * @Version  
  10.  * @ModifiedBy  
  11.  * @Copyright @ 2009 H&L Technology Inc.  
  12.  * @date 2009-12-31 下午02:16:25  
  13.  * @description 正则验证,只要是包含就返回true  
  14.  */  
  15. public class KSValidate {   
  16.   
  17.     // 合法字符正则表达式   
  18.     private final static String LAWFUL_EXPREG_STR = ".*(<script|select|update|delete|insert|').*";   
  19.     //用户名正则格式验证   
  20.     private final static String USER_PATTERN_STR="^[a-z0-9A-Z][a-zA-Z0-9_]{5,15}$";   
  21.     // 手机号码正则表达式   
  22.     private final static String MOBILETELL_EXPREG_STR = "^(?:13\\d|15[89])-?\\d{5}(\\d{3}|\\*{3})$";   
  23.     // 电话号码,传真正则表达式(兼容格式: 国家代码(4到5位)-区号(3到4位)-电话号码(7到8位)   
  24.     private final static String TELL_EXPREG_STR = "^(([0]\\d{3,4}-)?(0\\d{2,3})-)?(\\d{7,8})$";   
  25.     // QQ   
  26.     private final static String QQ_EXPREG_STR = "^[1-9]\\d{4,8}$";   
  27.     // 汉字   
  28.     private final static String CHINESE_EXPREG_STR = "^[\u4e00-\u9fa5]+$";   
  29.     // 邮箱   
  30.     private final static String EMAIL_EXPREG_STR = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$";   
  31.     // 邮编   
  32.     private final static String POSTCODE_EXPREG_STR = "^[1-9]\\d{5}(?!\\d)$";   
  33.     // 日期 yyyy-mm-dd   
  34.     private final static String DATE_EXPREG_STR = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";   
  35.     // 自然数   
  36.     private final static String NATURE_NUMBER = "^[1-9]\\d*|0$";   
  37.     // 非负整数   
  38.     private final static String SIGNED_INTEGER_NUMBER = "^\\d+$";   
  39.     //    
  40.     private static String SENSITIVITY_WORD = "sensitivityWord";   
  41.     private static String SENSITIVITY_WORD_ALL_TEXT = "sensitivityWord_allText";   
  42.     /**  
  43.      * 验证用户名格式是否正确,正确返回true  
  44.      * @author wangdawei  
  45.      * @date 2010-2-24 上午09:58:19  
  46.      * @param text  
  47.      * @return boolean  
  48.      */  
  49.     public static boolean isSpecialUserName(String text) {   
  50.         return matchingText(USER_PATTERN_STR, text);   
  51.     }   
  52.     /**  
  53.      * 验证是否是正整数,包含0  
  54.      *   
  55.      * @author 李英夫(2009-12-31 下午02:25:16)  
  56.      * @param inVal  
  57.      * @return boolean  
  58.      */  
  59.     public static boolean isSignedIntegerNumber(String text) {   
  60.         return matchingText(SIGNED_INTEGER_NUMBER, text);   
  61.     }   
  62.   
  63.     /**  
  64.      * 验证是否是数字,包含浮点数  
  65.      *   
  66.      * @author 李英夫(2009-12-31 下午02:22:16)  
  67.      * @param inVal  
  68.      * @return boolean  
  69.      */  
  70.     public static boolean isDigit(String inVal) {   
  71.         if (inVal == null || inVal.length() < 1)   
  72.             return false;   
  73.         boolean isRight = true;   
  74.         try {   
  75.             Float.parseFloat(inVal);   
  76.         } catch (NumberFormatException nfe) {   
  77.             isRight = false;   
  78.         }   
  79.         return isRight;   
  80.     }   
  81.   
  82.     /**  
  83.      * 是否存在非法字符,存在则返回true  
  84.      *   
  85.      * @author 李英夫(2009-12-31 下午02:26:16)  
  86.      * @param text  
  87.      * @return boolean  
  88.      */  
  89.     public static boolean isSpecialword(String text) {   
  90.         return matchingText(LAWFUL_EXPREG_STR, text);   
  91.     }   
  92.   
  93.     /**  
  94.      * 电话,正确则返回true  
  95.      *   
  96.      * @author 李英夫(2009-12-31 下午02:26:16)  
  97.      * @param text  
  98.      * @return boolean  
  99.      */  
  100.     public static boolean telPhone(String text) {   
  101.         return matchingText(TELL_EXPREG_STR, text);   
  102.     }   
  103.        
  104.     /**  
  105.      * 是否是手机号码  
  106.      *   
  107.      * @author 李英夫(2009-12-31 下午02:27:44)  
  108.      * @param text  
  109.      * @return boolean  
  110.      */  
  111.     public static boolean isMobilPhone(String text) {   
  112.         return matchingText(MOBILETELL_EXPREG_STR, text);   
  113.     }   
  114.   
  115.     /**  
  116.      * 是否是QQ  
  117.      *   
  118.      * @author 李英夫(2009-12-31 下午02:29:50)  
  119.      * @param text  
  120.      * @return boolean  
  121.      */  
  122.     public static boolean isQQ(String text) {   
  123.         return matchingText(QQ_EXPREG_STR, text);   
  124.     }   
  125.   
  126.     /**  
  127.      * 是否是汉字  
  128.      *   
  129.      * @author 李英夫(2009-12-31 下午02:31:53)  
  130.      * @param text  
  131.      * @return boolean  
  132.      */  
  133.     public static boolean isChinese(String text) {   
  134.         return matchingText(CHINESE_EXPREG_STR, text);   
  135.     }   
  136.   
  137.     /**  
  138.      * 是否是合法邮编  
  139.      *   
  140.      * @author 李英夫(2009-12-31 下午02:32:57)  
  141.      * @param text  
  142.      * @return boolean  
  143.      */  
  144.     public static boolean isPostCode(String text) {   
  145.         return matchingText(POSTCODE_EXPREG_STR, text);   
  146.     }   
  147.   
  148.     /**  
  149.      * 是否是电子邮件  
  150.      *   
  151.      * @author 李英夫(2009-12-31 下午02:33:49)  
  152.      * @param text  
  153.      * @return boolean  
  154.      */  
  155.     public static boolean isEMail(String text) {   
  156.         return matchingText(EMAIL_EXPREG_STR, text);   
  157.     }   
  158.   
  159.     /**  
  160.      * 是否是yyyy-MM-dd格式的日期  
  161.      *   
  162.      * @author 李英夫(2009-12-31 下午02:34:33)  
  163.      * @param text  
  164.      * @return boolean  
  165.      */  
  166.     public static boolean isDate(String text) {   
  167.         return matchingText(DATE_EXPREG_STR, text);   
  168.     }   
  169.   
  170.     /**  
  171.      * 是否是自然数  
  172.      * @author 李英夫(2009-12-31 下午02:35:24)  
  173.      * @param text  
  174.      * @return boolean  
  175.      */  
  176.     public static boolean isNatureNumber(String text) {   
  177.         return matchingText(NATURE_NUMBER, text);   
  178.     }   
  179.   
  180.     /**  
  181.      * 有敏感词返回true  
  182.      * @author 李英夫(2010-1-21 下午02:13:01)  
  183.      * @param text  
  184.      * @return boolean  
  185.      */  
  186.     public static boolean isSensitivityWord(String text){   
  187.         try {   
  188.             String sw = PropertiesLoader.getText(SENSITIVITY_WORD_ALL_TEXT);   
  189.             return matchingText(sw, text);   
  190.         } catch (Exception e) {   
  191.             e.printStackTrace();   
  192.             return true;   
  193.         }   
  194.     }   
  195.        
  196.     /**  
  197.      * 替换敏感词  
  198.      * @author 李英夫(2010-1-21 下午04:48:04)  
  199.      * @param text  
  200.      * @param newWord  
  201.      * @return String  
  202.      */  
  203.     public static String replaceSensitivityWord(String text, String newWord){   
  204.         try {   
  205.             Pattern p = Pattern.compile(PropertiesLoader.getText(SENSITIVITY_WORD)); // 正则表达式   
  206.             Matcher m = p.matcher(text); // 操作的字符串   
  207.             return m.replaceAll(newWord);   
  208.         } catch (Exception e) {   
  209.             e.printStackTrace();   
  210.             return "";   
  211.         }   
  212.     }   
  213.         /**  
  214.      * 正则验证  
  215.      * @author 李英夫(2009-12-31 下午02:20:16)  
  216.      * @param expression  
  217.      * @param text  
  218.      * @return boolean  
  219.      */  
  220.     public static boolean matchingText(String expression, String text) {   
  221.         boolean bool = false;   
  222.         if (expression != null && !"".equals(expression) && text != null  
  223.                 && !"".equals(text)) {   
  224.             Pattern p = Pattern.compile(expression); // 正则表达式   
  225.             Matcher m = p.matcher(text); // 操作的字符串   
  226.             bool = m.matches();   
  227.         }   
  228.         return bool;   
  229.     }   
  230.        
  231.     /**  
  232.      * 替换Html中的form标签、on..事件属性、href='javascript...'引用  
  233.      * @author 李英夫(2010-5-6 上午09:07:52)  
  234.      * @param src  
  235.      * @return String  
  236.      */  
  237.     public static String replaceFOH(String src){   
  238.         return src.replaceAll("<form.*?</form>|on[a-zA-z]+=([\"']?)[^\"']*\\1|href=([\"']?)javascript:[^\"']*\\2""");   
  239.     }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值