java常用正则校验工具类

正则常用校验工具类

 

  1 import java.util.regex.Pattern;
  2 
  3 /**
  4  * @program: 
  5  * @description: 校验工具类
  6  * @author: xujingyang
  7  * @create: 2019-01-05 16:21
  8  **/
  9 public class ValidatorUtil {
 10     /**
 11      * 正则表达式:验证用户名
 12      */
 13     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
 14 
 15     /**
 16      * 正则表达式:验证密码
 17      */
 18     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
 19 
 20     /**
 21      * 正则表达式:验证手机号
 22      */
 23     public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
 24 
 25     /**
 26      * 正则表达式:验证邮箱
 27      */
 28     public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
 29 
 30     /**
 31      * 正则表达式:验证汉字
 32      */
 33     public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
 34 
 35     /**
 36      * 正则表达式:验证身份证
 37      */
 38     public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
 39 
 40     /**
 41      * 正则表达式:验证URL
 42      */
 43     public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
 44 
 45     /**
 46      * 正则表达式:验证IP地址
 47      */
 48     public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
 49 
 50     /**
 51      * 校验用户名
 52      *
 53      * @param username
 54      * @return 校验通过返回true,否则返回false
 55      */
 56     public static boolean isUsername(String username) {
 57         return Pattern.matches(REGEX_USERNAME, username);
 58     }
 59 
 60     /**
 61      * 校验密码
 62      *
 63      * @param password
 64      * @return 校验通过返回true,否则返回false
 65      */
 66     public static boolean isPassword(String password) {
 67         return Pattern.matches(REGEX_PASSWORD, password);
 68     }
 69 
 70     /**
 71      * 校验手机号
 72      *
 73      * @param mobile
 74      * @return 校验通过返回true,否则返回false
 75      */
 76     public static boolean isMobile(String mobile) {
 77         return Pattern.matches(REGEX_MOBILE, mobile);
 78     }
 79 
 80     /**
 81      * 校验邮箱
 82      *
 83      * @param email
 84      * @return 校验通过返回true,否则返回false
 85      */
 86     public static boolean isEmail(String email) {
 87         return Pattern.matches(REGEX_EMAIL, email);
 88     }
 89 
 90     /**
 91      * 校验汉字
 92      *
 93      * @param chinese
 94      * @return 校验通过返回true,否则返回false
 95      */
 96     public static boolean isChinese(String chinese) {
 97         return Pattern.matches(REGEX_CHINESE, chinese);
 98     }
 99 
100     /**
101      * 校验身份证
102      *
103      * @param idCard
104      * @return 校验通过返回true,否则返回false
105      */
106     public static boolean isIDCard(String idCard) {
107         return Pattern.matches(REGEX_ID_CARD, idCard);
108     }
109 
110     /**
111      * 校验URL
112      *
113      * @param url
114      * @return 校验通过返回true,否则返回false
115      */
116     public static boolean isUrl(String url) {
117         return Pattern.matches(REGEX_URL, url);
118     }
119 
120     /**
121      * 校验IP地址
122      *
123      * @param ipAddr
124      * @return
125      */
126     public static boolean isIPAddr(String ipAddr) {
127         return Pattern.matches(REGEX_IP_ADDR, ipAddr);
128     }
129 
130 }

 

posted @ 2019-01-05 16:26  oldmonk  阅读(17406)  评论(0编辑  收藏  举报
Fork me on GitHub