JAVA学习代码——利用正则表达式判断手机/邮箱/身份证

来源:互联网 发布:mysql if else 编辑:程序博客网 时间:2024/05/17 09:00

来自:http://www.oschina.net/code/snippet_1021818_47946

用于校验用户名、密码、手机号、邮箱和身份证等信息

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.office.utility;  
  2.    
  3. import java.util.regex.Pattern;  
  4.    
  5. /** 
  6.  * 校验器:利用正则表达式校验邮箱、手机号等 
  7.  *  
  8.  * @author liujiduo 
  9.  *  
  10.  */  
  11. public class Validator {  
  12.     /** 
  13.      * 正则表达式:验证用户名 
  14.      */  
  15.     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";  
  16.    
  17.     /** 
  18.      * 正则表达式:验证密码 
  19.      */  
  20.     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";  
  21.    
  22.     /** 
  23.      * 正则表达式:验证手机号 
  24.      */  
  25.     public static final String REGEX_MOBILE = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";  
  26.    
  27.     /** 
  28.      * 正则表达式:验证邮箱 
  29.      */  
  30.     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,}$";  
  31.    
  32.     /** 
  33.      * 正则表达式:验证汉字 
  34.      */  
  35.     public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";  
  36.    
  37.     /** 
  38.      * 正则表达式:验证身份证 
  39.      */  
  40.     public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";  
  41.    
  42.     /** 
  43.      * 正则表达式:验证URL 
  44.      */  
  45.     public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";  
  46.    
  47.     /** 
  48.      * 正则表达式:验证IP地址 
  49.      */  
  50.     public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";  
  51.    
  52.     /** 
  53.      * 校验用户名 
  54.      *  
  55.      * @param username 
  56.      * @return 校验通过返回true,否则返回false 
  57.      */  
  58.     public static boolean isUsername(String username) {  
  59.         return Pattern.matches(REGEX_USERNAME, username);  
  60.     }  
  61.    
  62.     /** 
  63.      * 校验密码 
  64.      *  
  65.      * @param password 
  66.      * @return 校验通过返回true,否则返回false 
  67.      */  
  68.     public static boolean isPassword(String password) {  
  69.         return Pattern.matches(REGEX_PASSWORD, password);  
  70.     }  
  71.    
  72.     /** 
  73.      * 校验手机号 
  74.      *  
  75.      * @param mobile 
  76.      * @return 校验通过返回true,否则返回false 
  77.      */  
  78.     public static boolean isMobile(String mobile) {  
  79.         return Pattern.matches(REGEX_MOBILE, mobile);  
  80.     }  
  81.    
  82.     /** 
  83.      * 校验邮箱 
  84.      *  
  85.      * @param email 
  86.      * @return 校验通过返回true,否则返回false 
  87.      */  
  88.     public static boolean isEmail(String email) {  
  89.         return Pattern.matches(REGEX_EMAIL, email);  
  90.     }  
  91.    
  92.     /** 
  93.      * 校验汉字 
  94.      *  
  95.      * @param chinese 
  96.      * @return 校验通过返回true,否则返回false 
  97.      */  
  98.     public static boolean isChinese(String chinese) {  
  99.         return Pattern.matches(REGEX_CHINESE, chinese);  
  100.     }  
  101.    
  102.     /** 
  103.      * 校验身份证 
  104.      *  
  105.      * @param idCard 
  106.      * @return 校验通过返回true,否则返回false 
  107.      */  
  108.     public static boolean isIDCard(String idCard) {  
  109.         return Pattern.matches(REGEX_ID_CARD, idCard);  
  110.     }  
  111.    
  112.     /** 
  113.      * 校验URL 
  114.      *  
  115.      * @param url 
  116.      * @return 校验通过返回true,否则返回false 
  117.      */  
  118.     public static boolean isUrl(String url) {  
  119.         return Pattern.matches(REGEX_URL, url);  
  120.     }  
  121.    
  122.     /** 
  123.      * 校验IP地址 
  124.      *  
  125.      * @param ipAddr 
  126.      * @return 
  127.      */  
  128.     public static boolean isIPAddr(String ipAddr) {  
  129.         return Pattern.matches(REGEX_IP_ADDR, ipAddr);  
  130.     }  
  131.    
  132.     public static void main(String[] args) {  
  133.         String username = "fdsdfsdj";  
  134.         System.out.println(Validator.isUsername(username));  
  135.         System.out.println(Validator.isChinese(username));  
  136.     }  
  137. }  
0 0
原创粉丝点击