码迷,mamicode.com
首页 > 移动开发 > 详细

Android 中正则表达式工具类

时间:2018-06-21 14:43:30      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:suse   name   pre   中文   邮箱   pass   ali   url   用户   

  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 }

 

Android 中正则表达式工具类

标签:suse   name   pre   中文   邮箱   pass   ali   url   用户   

原文地址:https://www.cnblogs.com/monkey0928/p/9208859.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!