正则表达式工具类,正则表达式封装,Java正则表达式

来源:互联网 发布:怎么在淘宝卖二手东西 编辑:程序博客网 时间:2024/04/28 16:05
Java代码  收藏代码
  1. package com.chinagas.org.common.utils;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6.   
  7. public final class RegUtils {  
  8.   
  9.     /*------------------ 正则表达式 ---------------------*/  
  10.     /** 
  11.      * 邮箱 
  12.      */  
  13.     public static final String REGEX_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";  
  14.     /** 
  15.      * 手机号码 
  16.      */  
  17.     public static final String REGEX_PHONE = "^13[0-9]{9}|15[012356789][0-9]{8}|18[0-9]{9}|(14[57][0-9]{8})|(17[015678][0-9]{8})$";  
  18.     /** 
  19.      * 仅中文 
  20.      */  
  21.     public static final String REGEX_CHINESE = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";  
  22.     /** 
  23.      * 整数 
  24.      */  
  25.     public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";  
  26.     /** 
  27.      * 数字 
  28.      */  
  29.     public static final String REGEX_NUMBER = "^([+-]?)\\d*\\.?\\d+$";  
  30.     /** 
  31.      * 正整数 
  32.      */  
  33.     public static final String REGEX_INTEGER_POS = "^[1-9]\\d*$";  
  34.     /** 
  35.      * 浮点数 
  36.      */  
  37.     public static final String REGEX_FLOAT = "^([+-]?)\\d*\\.\\d+$";  
  38.     /** 
  39.      * 正浮点数 
  40.      */  
  41.     public static final String REGEX_FLOAT_POS = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";  
  42.     /** 
  43.      * 字母 
  44.      */  
  45.     public static final String REGEX_LETTER = "^[A-Za-z]+$";  
  46.     /** 
  47.      * 大写字母 
  48.      */  
  49.     public static final String REGEX_LETTER_UPPERCASE = "^[A-Z]+$";  
  50.     /** 
  51.      * 小写字母 
  52.      */  
  53.     public static final String REGEX_LETTER_LOWERCASE = "^[a-z]+$";  
  54.     /** 
  55.      * 邮编 
  56.      */  
  57.     public static final String REGEX_ZIPCODE = "^\\d{6}$";  
  58.     /** 
  59.      * ip v4地址 
  60.      */  
  61.     public static final String REGEX_IP4 = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";  
  62.     /** 
  63.      * 图片 
  64.      */  
  65.     public static final String REGEX_PICTURE = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";/** 
  66.     /** 
  67.      * 压缩文件 
  68.      */  
  69.     public static final String REGEX_RAR = "(.*)\\.(rar|zip|7zip|tgz)$";  
  70.     /** 
  71.      * QQ号码,最短5位,最长15位数字 
  72.      */  
  73.     public static final String REGEX_QQ = "^[1-9]\\d{4,14}$";  
  74.     /** 
  75.      * 日期(yyyy-MM-dd) 
  76.      */  
  77.     public static final String REGEX_DATE = "^\\d{4}\\D+\\d{2}\\D+\\d{2}$";  
  78.     /** 
  79.      * 日期(yyyy-MM-dd),精确,能检查到2月及31号 
  80.      */  
  81.     public static final String REGEX_DATE_PRECISE = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";  
  82.     /** 
  83.      * 时间(HH:mm:ss或HH:mm) 
  84.      */  
  85.     public static final String REGEX_TIME = "^((([0-1][0-9])|2[0-3]):[0-5][0-9])(:[0-5][0-9])?$";  
  86.       
  87.     /** 
  88.      * 校验手机号码 
  89.      * @param mobile 
  90.      * @return 
  91.      * @author lqyao 
  92.      */  
  93.     public static final boolean isMoblie(String mobile){  
  94.         boolean flag = false;  
  95.         if (null != mobile && !mobile.trim().equals("") && mobile.trim().length() == 11) {  
  96.             Pattern pattern = Pattern.compile(REGEX_PHONE);  
  97.             Matcher matcher = pattern.matcher(mobile.trim());  
  98.             flag = matcher.matches();  
  99.         }  
  100.         return flag;  
  101.     }  
  102.       
  103.     /** 
  104.      * 校验邮箱 
  105.      * @param value 
  106.      * @return 
  107.      * @author lqyao 
  108.      */  
  109.     public static final boolean isEmail(String value){  
  110.         boolean flag = false;  
  111.         if (null != value && !value.trim().equals("")) {  
  112.             Pattern pattern = Pattern.compile(REGEX_EMAIL);  
  113.             Matcher matcher = pattern.matcher(value.trim());  
  114.             flag = matcher.matches();  
  115.         }  
  116.         return flag;  
  117.     }  
  118.       
  119.     /** 
  120.      * 校验密码 
  121.      * @param password 
  122.      * @return 长度符合返回true,否则为false 
  123.      * @author lqyao 
  124.      * @since 2015-09-24 
  125.      */  
  126.     public static final boolean isPassword(String password){  
  127.         boolean flag = false;  
  128.         if (null != password && !password.trim().equals("")) {  
  129.             password = password.trim();  
  130.             if(password.length() >= 6 && password.length() <= 30){  
  131.                 return true;  
  132.             }  
  133.         }  
  134.         return flag;  
  135.     }  
  136.       
  137.     /** 
  138.      * 校验手机验证码 
  139.      * @param value 
  140.      * @return 符合正则表达式返回true,否则返回false 
  141.      * @author lqyao 
  142.      * @since 2015-09-24 
  143.      */  
  144.     public static final boolean isPhoneValidateCode(String value){  
  145.         boolean flag = false;  
  146.         if (null != value && !value.trim().equals("")) {  
  147.             Pattern pattern = Pattern.compile("^8\\d{5}$");  
  148.             Matcher matcher = pattern.matcher(value.trim());  
  149.             flag = matcher.matches();  
  150.         }  
  151.         return flag;  
  152.     }  
  153.       
  154.     /** 
  155.      * 正则表达式校验,符合返回True 
  156.      * @param regex 正则表达式 
  157.      * @param content 校验的内容 
  158.      * @return 
  159.      * @author lqy 
  160.      */  
  161.     public static boolean isMatch(String regex, CharSequence content){  
  162.         return Pattern.matches(regex, content);  
  163.     }  
  164.       
  165.     public static boolean isUpperCase(String str){  
  166.         if(StrUtils.isEmpty(str)){  
  167.             return false;  
  168.         }  
  169.         String reg = "^[A-Z]$";  
  170.         return isMatch(reg,str);  
  171.     }  
  172. }  
0 0
原创粉丝点击