常用的Regex工具类

这里整理了部分,特别是日期时间和身份证校验,功能相当强大,值得学习

  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.regex.Pattern;
  4. public class RegexUtils {
  5.     private static Pattern PATTERN_EMAIL = Pattern
  6.             .compile("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");// 邮件地址
  7.     private static Pattern PATTERN_TEL = Pattern
  8.             .compile("^([0-9]{3,4}-)?[0-9]{7,8}$");// 固定电话
  9.     private static Pattern PATTERN_MOBILE = Pattern
  10.             .compile("^(\\+86)?0?1[3|5]\\d{9}$");// 移动电话
  11.     private static Pattern PATTERN_ALPHA = Pattern.compile("^[A-Za-z]+$");// 字母
  12.     private static Pattern PATTERN_DIGITAL = Pattern.compile("^\\d+$");// 数字
  13.     private static Pattern PATTERN_CHINESE = Pattern
  14.             .compile("^[\\u4E00-\\u9FA5]+$");// 中文
  15.     private static Pattern PATTERN_IDCARD_15 = Pattern
  16.             .compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");// 15位身份证格式
  17.     private static Pattern PATTERN_IDCARD_18 = Pattern
  18.             .compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}[\\d|x|X]$");// 18位身份证格式
  19.     private static Pattern PATTERN_IP = Pattern
  20.             .compile("^((00\\d|1?\\d?\\d|(2([0-4]\\d|5[0-5])))\\.){3}(00\\d|1?\\d?\\d|(2([0-4]\\d|5[0-5])))$");//IP格式
  21.     private static Pattern PATTERN_TIME = Pattern
  22.             .compile("((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])(\\:([0-5]?[0-9]))");//时间格式
  23.     private static Pattern PATTERN_REPEAT = Pattern.compile(".*(.).*\\1.*");//重复字符格式
  24.     public RegexUtils() {
  25.     }
  26.     /**
  27.      * 校验email格式
  28.      * 
  29.      * @param email
  30.      *            email
  31.      * @return
  32.      */
  33.     public static boolean isEmail(String email) {
  34.         if (email == null)
  35.             return false;
  36.         else
  37.             return PATTERN_EMAIL.matcher(email).matches();
  38.     }
  39.     public static boolean isTelephone(String telephone) {
  40.         if (telephone == null)
  41.             return false;
  42.         else
  43.             return PATTERN_TEL.matcher(telephone).matches();
  44.     }
  45.     public static boolean isMobile(String mobile) {
  46.         if (mobile == null)
  47.             return false;
  48.         else
  49.             return PATTERN_MOBILE.matcher(mobile).matches();
  50.     }
  51.     public static boolean isAlpha(String alpha) {
  52.         if (alpha == null)
  53.             return false;
  54.         else
  55.             return PATTERN_ALPHA.matcher(alpha).matches();
  56.     }
  57.     public static boolean isDigital(String digital) {
  58.         if (digital == null)
  59.             return false;
  60.         else
  61.             return PATTERN_DIGITAL.matcher(digital).matches();
  62.     }
  63.     public static boolean isChinese(String chinese) {
  64.         if (chinese == null)
  65.             return false;
  66.         else
  67.             return PATTERN_CHINESE.matcher(chinese).matches();
  68.     }
  69.     public static boolean isDateTime(String dateTime) {
  70.         return isDateTime(dateTime, "-");
  71.     }
  72.     /**
  73.      * 校验日期时间 月 日 时间 可以不写前面的0 ,例如 2008-9-2 3:9:1
  74.      * 
  75.      * @param dateTime
  76.      * @param partition
  77.      *            分隔符 这里只支持 \、/、-、空格 四类分隔符,如果是长度大于1,则取第一个字符作为分隔符
  78.      * @return 如何符合格式,返回true
  79.      */
  80.     public static boolean isDateTime(String dateTime, String partition) {
  81.         if (dateTime == null || partition == null || "".equals(partition))
  82.             return false;
  83.         String s = "";
  84.         char split = partition.charAt(0);
  85.         if (split != '\\' && split != '/' && split != '-' && split != ' ')
  86.             throw new IllegalArgumentException((new StringBuilder(
  87.                     "partition can not start with '")).append(partition)
  88.                     .append("'!").toString());
  89.         s = (new StringBuilder(String.valueOf(s))).append(split).toString();
  90.         StringBuilder part = new StringBuilder(
  91.                 "^((\\d{2}(([02468][048])|([13579][26]))");
  92.         part.append(s);
  93.         part.append("((((0?[13578]");
  94.         part.append(")|(1[02]))");
  95.         part.append(s);
  96.         part.append("((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4");
  97.         part.append("69])|(11))");
  98.         part.append(s);
  99.         part.append("((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\");
  100.         part
  101.                 .append("s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([1");
  102.         part.append("3579][01345789]))");
  103.         part.append(s);
  104.         part.append("((((0?[13578])|(1[02]))");
  105.         part.append(s);
  106.         part.append("((");
  107.         part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
  108.         part.append(s);
  109.         part.append("((");
  110.         part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
  111.         part.append(s);
  112.         part.append("((");
  113.         part.append("0?[1-9])|([1-2][0-9])|(30)))|(0?2");
  114.         part.append(s);
  115.         part.append("((0?[1-9])|(1[0-9])|(2[0-8]))))))");
  116.         part
  117.                 .append("(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])\\:([0-5]?[0-9])))?");
  118.         return Pattern.matches(part.toString(), dateTime);
  119.     }
  120.     public static boolean isDate(String date) {
  121.         return isDate(date, "-");
  122.     }
  123.     /**
  124.      * 校验日期 月 日 可以不写前面的0 ,例如 2008-9-2 3:9:1
  125.      * 
  126.      * @param date
  127.      * @param partition
  128.      *            分隔符 这里只支持 \、/、-、空格 四类分隔符,如果是长度大于1,则取第一个字符作为分隔符
  129.      * @return 如何符合格式,返回true
  130.      */
  131.     public static boolean isDate(String date, String partition) {
  132.         if (date == null || partition == null || "".equals(partition))
  133.             return false;
  134.         String s = "";
  135.         char split = partition.charAt(0);
  136.         if (split != '\\' && split != '/' && split != '-' && split != ' ')
  137.             throw new IllegalArgumentException((new StringBuilder(
  138.                     "partition can not start with '")).append(partition)
  139.                     .append("'!").toString());
  140.         s = (new StringBuilder(String.valueOf(s))).append(split).toString();
  141.         StringBuilder part = new StringBuilder(
  142.                 "^((\\d{2}(([02468][048])|([13579][26]))");
  143.         part.append(s);
  144.         part.append("((((0?[13578]");
  145.         part.append(")|(1[02]))");
  146.         part.append(s);
  147.         part.append("((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4");
  148.         part.append("69])|(11))");
  149.         part.append(s);
  150.         part.append("((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\");
  151.         part
  152.                 .append("s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([1");
  153.         part.append("3579][01345789]))");
  154.         part.append(s);
  155.         part.append("((((0?[13578])|(1[02]))");
  156.         part.append(s);
  157.         part.append("((");
  158.         part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
  159.         part.append(s);
  160.         part.append("((");
  161.         part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
  162.         part.append(s);
  163.         part.append("((");
  164.         part.append("0?[1-9])|([1-2][0-9])|(30)))|(0?2");
  165.         part.append(s);
  166.         part.append("((0?[1-9])|(1[0-9])|(2[0-8]))))))$");
  167.         return Pattern.matches(part.toString(), date);
  168.     }
  169.     /**
  170.      * 校验时间 时间可以不写前面的0 如 9:3:1
  171.      * 
  172.      * @param time
  173.      *            时间
  174.      * @return
  175.      */
  176.     public static boolean isTime(String time) {
  177.         if (time == null)
  178.             return false;
  179.         else
  180.             return PATTERN_TIME.matcher(time).matches();
  181.     }
  182.     /**
  183.      * 严格校验身份证的格式,支持15位和18位校验
  184.      * 
  185.      * @param card
  186.      * @return
  187.      */
  188.     public static boolean isIdCard(String card) {
  189.         if (card == null)
  190.             return false;
  191.         int length = card.length();
  192.         if (length == 15) {// 15位号码身份证
  193.             if (!PATTERN_IDCARD_15.matcher(card).matches())
  194.                 return false;
  195.             if (!addressCode.containsKey(card.substring(02)))
  196.                 return false;
  197.             String birthday = (new StringBuilder("19")).append(
  198.                     card.substring(68)).append("-").append(
  199.                     card.substring(810)).append("-").append(
  200.                     card.substring(1012)).toString();
  201.             if (!isDate(birthday))
  202.                 return false;
  203.         } else if (length == 18) {// 18位号码身份证
  204.             if (!PATTERN_IDCARD_18.matcher(card).matches())// 格式不匹配
  205.                 return false;
  206.             if (!addressCode.containsKey(card.substring(02)))// 不符合地区码
  207.                 return false;
  208.             String birthday = (new StringBuilder(card.substring(610)))
  209.                     .append("-").append(card.substring(1012)).append("-")
  210.                     .append(card.substring(1214)).toString();
  211.             if (!isDate(birthday))
  212.                 return false;
  213.             int sum = 0;
  214.             for (int i = 0; i < length - 1; i++)
  215.                 sum += (card.charAt(i) - 48) * idCoefficient[i];
  216.             char mod = idMod[sum % 11];
  217.             if (mod != Character.toLowerCase(card.charAt(17)))
  218.                 return false;
  219.         } else {
  220.             return false;
  221.         }
  222.         return true;
  223.     }
  224.    
  225.     /**
  226.      * 校验IP格式
  227.      * @param ip
  228.      * @return
  229.      */
  230.     public static boolean isIP(String ip) {
  231.         if (ip == null)
  232.             return false;
  233.         else
  234.             return PATTERN_IP.matcher(ip).matches();
  235.     }
  236.     
  237.     /**
  238.      * 校验是否有重复字符
  239.      * @param repeat
  240.      * @return
  241.      */
  242.     public static boolean hasRepeat(String repeat) {
  243.         if (repeat == null)
  244.             return false;
  245.         else
  246.             return PATTERN_REPEAT.matcher(repeat).matches();
  247.     }
  248.     public static Map<String, String> addressCode;
  249.     public static int idCoefficient[] = { 7910584216379,
  250.             105842 };
  251.     public static char idMod[] = { '1''0''x''9''8''7''6''5''4',
  252.             '3''2' };
  253.     static {
  254.         addressCode = new HashMap<String, String>(35);
  255.         addressCode.put("11""\u5317\u4EAC");
  256.         addressCode.put("12""\u5929\u6D25");
  257.         addressCode.put("13""\u6CB3\u5317");
  258.         addressCode.put("14""\u5C71\u897F");
  259.         addressCode.put("15""\u5185\u8499\u53E4");
  260.         addressCode.put("21""\u8FBD\u5B81");
  261.         addressCode.put("22""\u5409\u6797");
  262.         addressCode.put("23""\u9ED1\u9F99\u6C5F");
  263.         addressCode.put("31""\u4E0A\u6D77");
  264.         addressCode.put("32""\u6C5F\u82CF");
  265.         addressCode.put("33""\u6D59\u6C5F");
  266.         addressCode.put("34""\u5B89\u5FBD");
  267.         addressCode.put("35""\u798F\u5EFA");
  268.         addressCode.put("36""\u6C5F\u897F");
  269.         addressCode.put("37""\u5C71\u4E1C");
  270.         addressCode.put("41""\u6CB3\u5357");
  271.         addressCode.put("42""\u6E56\u5317");
  272.         addressCode.put("43""\u6E56\u5357");
  273.         addressCode.put("44""\u5E7F\u4E1C");
  274.         addressCode.put("45""\u5E7F\u897F");
  275.         addressCode.put("46""\u6D77\u5357");
  276.         addressCode.put("50""\u91CD\u5E86");
  277.         addressCode.put("51""\u56DB\u5DDD");
  278.         addressCode.put("52""\u8D35\u5DDE");
  279.         addressCode.put("53""\u4E91\u5357");
  280.         addressCode.put("54""\u897F\u85CF");
  281.         addressCode.put("61""\u9655\u897F");
  282.         addressCode.put("62""\u7518\u8083");
  283.         addressCode.put("63""\u9752\u6D77");
  284.         addressCode.put("64""\u5B81\u590F");
  285.         addressCode.put("65""\u65B0\u7586");
  286.         addressCode.put("71""\u53F0\u6E7E");
  287.         addressCode.put("81""\u9999\u6E2F");
  288.         addressCode.put("82""\u6FB3\u95E8");
  289.         addressCode.put("91""\u56FD\u5916");
  290.     }
  291. }
posted @ 2008-11-29 16:55  罗志威  阅读(335)  评论(0编辑  收藏  举报