正则: 密码 邮箱 汉字 数字 身份证 车牌号 手机号 非法字符等

  1. public class RegexUtil {  
  2.     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";  
  3.     //     * 正则表达式:验证密码  
  4.     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";  
  5.     //     * 正则表达式:验证手机号  
  6.     public static final String REGEX_MOBILE = "^1([3]|[5]|[8])\\d{9}$";  
  7.     //     * 正则表达式:验证邮箱  
  8.     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,}$";  
  9.     //     * 正则表达式:验证汉字  
  10.     public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";  
  11.     //     * 正则表达式:验证车牌号  
  12.     public static final String REGEX_CarNo = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$";  
  13.     //     * 正则表达式:验证大于0数字  
  14.     public static final String REGEX_NUM = "^[1-9]\\d*$";  
  15.     //     * 正则表达式:验证身份证  
  16.     public static final String REGEX_ID_CARD = "(^([1-9]{1}\\d{5}[1-2]{1}[09]{1}\\d{2}(([0]{1}[1-9]{1})|([1]{1}[012]{1}))(([0]{1}[1-9]{1})|([12]{1}\\d{1})|([3]{1}[01]{1}))(\\d{4}|(\\d{3}[X]{1})))$)|(^([1-9]{1}\\d{5}\\d{2}(([0]{1}[1-9]{1})|([1]{1}[012]{1}))(([0]{1}[1-9]{1})|([12]{1}\\d{1})|([3]{1}[01]{1}))\\d{3})$)";  
  17.     //     * 正则表达式:验证URL  
  18.     public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w\\-\\ ./?%&=]*)?";  
  19.     //     * 正则表达式:验证IP地址  
  20.     public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";  
  21.     //     * 正则表达式:姓名是2-4字的汉字  
  22.     public static final String REGEX_ChineseName = "^[\\u4e00-\\u9fa5]{2,4}$";  
  23.   
  24.     //     * 校验非法字符  
  25.     public static boolean isUnRuleText(String input) {  
  26.         if (input.toString().matches(".*[/\\\\:*?\"<>|\t].*") || input.toString().matches(".*\\p{So}.*")) {  
  27.             return true;  
  28.         }  
  29.         return false;  
  30.     }  
  31.   
  32.     public static boolean isChineseName(String chineseName) {  
  33.         return Pattern.matches(REGEX_ChineseName, chineseName);  
  34.     }  
  35.   
  36.     //     * 校验用户名  
  37.     public static boolean isUsername(String username) {  
  38.         return Pattern.matches(REGEX_USERNAME, username);  
  39.     }  
  40.   
  41.     //     * 校验纯数字  
  42.     public static boolean isNums(String nums) {  
  43.         return Pattern.matches(REGEX_NUM, nums);  
  44.     }  
  45.   
  46.     //     * 校验密码  
  47.     public static boolean isPassword(String password) {  
  48.         return Pattern.matches(REGEX_PASSWORD, password);  
  49.     }  
  50.   
  51.     //     * 校验手机号  
  52.     public static boolean isMobile(String mobile) {  
  53.         return Pattern.matches(REGEX_MOBILE, mobile);  
  54.     }  
  55.   
  56.     //     * 校验车牌号  
  57.     public static boolean isCarNo(String car_no) {  
  58.         return Pattern.matches(REGEX_CarNo, car_no);  
  59.     }  
  60.   
  61.     //     * 校验邮箱  
  62.     public static boolean isEmail(String email) {  
  63.         return Pattern.matches(REGEX_EMAIL, email);  
  64.     }  
  65.   
  66.     //     * 校验汉字  
  67.     public static boolean isChinese(String chinese) {  
  68.         return Pattern.matches(REGEX_CHINESE, chinese);  
  69.     }  
  70.   
  71.     //     * 校验身份证  
  72.     public static boolean isIDCard(String idCard) {  
  73.         return Pattern.matches(REGEX_ID_CARD, idCard);  
  74.     }  
  75.   
  76.     //     * 校验URL  
  77.     public static boolean isUrl(String url) {  
  78.         return Pattern.matches(REGEX_URL, url);  
  79.     }  
  80.   
  81.     //     * 校验IP地址  
  82.     public static boolean isIPAddr(String ipAddr) {  
  83.         return Pattern.matches(REGEX_IP_ADDR, ipAddr);  
  84.     }  
  85. }  

猜你喜欢

转载自blog.csdn.net/chinassj/article/details/79501118