Android 中正則表達式工具類


  1 package com.example.administrator.magiclamp.utils;
  2 
  3 import java.util.regex.Pattern;
  4 
  5 /**
  6  * 校驗器:利用正則表達式校驗郵箱、手機號等
  7  * @author Mr.duan
  8  */
  9 public class Validator {
 10     /**
 11      * 正則表達式:驗證用戶名(不包含中文和特殊字符)如果用戶名使用手機號碼或郵箱 則結合手機號驗證和郵箱驗證
 12      */
 13     public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";
 14  
 15     /**
 16      * 正則表達式:驗證密碼(不包含特殊字符)
 17      */
 18     public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{1,12}$";
 19  
 20     /**
 21      * 正則表達式:驗證手機號
 22      */
 23     public static final String REGEX_MOBILE = "^((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      * 正則表達式:驗證漢字(1-9個漢字)  {1,9} 自定義區間
 32      */
 33     public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5]{1,9}$";
 34  
 35     /**
 36      * 正則表達式:驗證身份證
 37      */
 38     public static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
 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 = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
 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 ipAddress
124      * @return
125      */
126     public static boolean isIPAddress(String ipAddress) {
127         return Pattern.matches(REGEX_IP_ADDR, ipAddress);
128     }
129    
130 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM