android常用正则工具类

此类提供日常开发中常用的正则验证函数,比如:邮箱、手机号、电话号码、身份证号码、日期、数字、小数、URL、IP地址等。使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        return m.matches();
每个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论。下面是工具类的完整代码:

 

源码地址http://download.csdn.net/detail/u014608640/7316565 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /**  
  2.  * 正则工具类  
  3.  * 提供验证邮箱、手机号、电话号码、身份证号码、数字等方法  
  4.  */  
  5. public final class RegexUtils {  
  6.   
  7.     /**  
  8.      * 验证Email  
  9.      * @param email email地址,格式:zhangsan@sina.com,zhangsan@xxx.com.cn,xxx代表邮件服务商  
  10.      * @return 验证成功返回true,验证失败返回false  
  11.      */  
  12.     public static boolean checkEmail(String email) {  
  13.         String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";  
  14.         return Pattern.matches(regex, email);  
  15.     }  
  16.       
  17.     /**  
  18.      * 验证身份证号码  
  19.      * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母  
  20.      * @return 验证成功返回true,验证失败返回false  
  21.      */  
  22.     public static boolean checkIdCard(String idCard) {  
  23.         String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";  
  24.         return Pattern.matches(regex,idCard);  
  25.     }  
  26.       
  27.     /**  
  28.      * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))  
  29.      * @param mobile 移动、联通、电信运营商的号码段  
  30.      *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)  
  31.      *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>  
  32.      *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>  
  33.      *<p>电信的号段:133、153、180(未启用)、189</p>  
  34.      * @return 验证成功返回true,验证失败返回false  
  35.      */  
  36.     public static boolean checkMobile(String mobile) {  
  37.         String regex = "(\\+\\d+)?1[3458]\\d{9}$";  
  38.         return Pattern.matches(regex,mobile);  
  39.     }  
  40.       
  41.     /**  
  42.      * 验证固定电话号码  
  43.      * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447  
  44.      * <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,  
  45.      *  数字之后是空格分隔的国家(地区)代码。</p>  
  46.      * <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——  
  47.      * 对不使用地区或城市代码的国家(地区),则省略该组件。</p>  
  48.      * <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>  
  49.      * @return 验证成功返回true,验证失败返回false  
  50.      */  
  51.     public static boolean checkPhone(String phone) {  
  52.         String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";  
  53.         return Pattern.matches(regex, phone);  
  54.     }  
  55.       
  56.     /**  
  57.      * 验证整数(正整数和负整数)  
  58.      * @param digit 一位或多位0-9之间的整数  
  59.      * @return 验证成功返回true,验证失败返回false  
  60.      */  
  61.     public static boolean checkDigit(String digit) {  
  62.         String regex = "\\-?[1-9]\\d+";  
  63.         return Pattern.matches(regex,digit);  
  64.     }  
  65.       
  66.     /**  
  67.      * 验证整数和浮点数(正负整数和正负浮点数)  
  68.      * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30  
  69.      * @return 验证成功返回true,验证失败返回false  
  70.      */  
  71.     public static boolean checkDecimals(String decimals) {  
  72.         String regex = "\\-?[1-9]\\d+(\\.\\d+)?";  
  73.         return Pattern.matches(regex,decimals);  
  74.     }   
  75.       
  76.     /**  
  77.      * 验证空白字符  
  78.      * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B  
  79.      * @return 验证成功返回true,验证失败返回false  
  80.      */  
  81.     public static boolean checkBlankSpace(String blankSpace) {  
  82.         String regex = "\\s+";  
  83.         return Pattern.matches(regex,blankSpace);  
  84.     }  
  85.       
  86.     /**  
  87.      * 验证中文  
  88.      * @param chinese 中文字符  
  89.      * @return 验证成功返回true,验证失败返回false  
  90.      */  
  91.     public static boolean checkChinese(String chinese) {  
  92.         String regex = "^[\u4E00-\u9FA5]+$";  
  93.         return Pattern.matches(regex,chinese);  
  94.     }  
  95.       
  96.     /**  
  97.      * 验证日期(年月日)  
  98.      * @param birthday 日期,格式:1992-09-03,或1992.09.03  
  99.      * @return 验证成功返回true,验证失败返回false  
  100.      */  
  101.     public static boolean checkBirthday(String birthday) {  
  102.         String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";  
  103.         return Pattern.matches(regex,birthday);  
  104.     }  
  105.       
  106.     /**  
  107.      * 验证URL地址  
  108.      * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80  
  109.      * @return 验证成功返回true,验证失败返回false  
  110.      */  
  111.     public static boolean checkURL(String url) {  
  112.         String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";  
  113.         return Pattern.matches(regex, url);  
  114.     }  
  115.       
  116.     /**  
  117.      * 匹配中国邮政编码  
  118.      * @param postcode 邮政编码  
  119.      * @return 验证成功返回true,验证失败返回false  
  120.      */  
  121.     public static boolean checkPostcode(String postcode) {  
  122.         String regex = "[1-9]\\d{5}";  
  123.         return Pattern.matches(regex, postcode);  
  124.     }  
  125.       
  126.     /**  
  127.      * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)  
  128.      * @param ipAddress IPv4标准地址  
  129.      * @return 验证成功返回true,验证失败返回false  
  130.      */  
  131.     public static boolean checkIpAddress(String ipAddress) {  
  132.         String regex = "[1-9](\\d{1,2})?\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))\\.(0|([1-9](\\d{1,2})?))";  
  133.         return Pattern.matches(regex, ipAddress);  
  134.     }  
  135.       
  136. }  


 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  * 正则表达式工具类测试  
  2.  */  
  3. public class RegexUtilsTest {  
  4.       
  5.     /**  
  6.      * 验证邮箱  
  7.      */  
  8.     @Test  
  9.     public void testCheckEmail() {  
  10.         boolean result = RegexUtils.checkEmail("zha2_ngsan@sina.com.cn");  
  11.         Assert.assertTrue(result);  
  12.     }  
  13.       
  14.     /**  
  15.      * 验证身份证号码  
  16.      */  
  17.     @Test  
  18.     public void testCheckIdCard() {  
  19.         boolean result = RegexUtils.checkIdCard("432403193902273273");  
  20.         Assert.assertTrue(result);  
  21.     }  
  22.       
  23.     /**  
  24.      * 验证手机号码  
  25.      */  
  26.     @Test  
  27.     public void testCheckMobile() {  
  28.         boolean result = RegexUtils.checkMobile("+8613620285733");  
  29.         Assert.assertTrue(result);  
  30.     }  
  31.       
  32.     /**  
  33.      * 验证电话号码  
  34.      */  
  35.     @Test  
  36.     public void testCheckPhone() {  
  37.         boolean result = RegexUtils.checkPhone("+860738-4630706");  
  38.         Assert.assertTrue(result);  
  39.     }  
  40.       
  41.     /**  
  42.      * 验证整数(正整数和负整数)  
  43.      */  
  44.     @Test  
  45.     public void testCheckDigit() {  
  46.         boolean result = RegexUtils.checkDigit("123132");  
  47.         Assert.assertTrue(result);  
  48.     }  
  49.       
  50.     /**  
  51.      * 验证小数和整数(正负整数和正负小数)  
  52.      */  
  53.     @Test  
  54.     public void testCheckDecimals() {  
  55.         boolean result = RegexUtils.checkDecimals("-33.2");  
  56.         Assert.assertTrue(result);  
  57.     }  
  58.       
  59.     /**  
  60.      * 验证空白字符  
  61.      */  
  62.     @Test  
  63.     public void testCheckBlankSpace() {  
  64.         boolean result = RegexUtils.checkBlankSpace("           ");  
  65.         Assert.assertTrue(result);  
  66.     }  
  67.       
  68.     /**  
  69.      * 匹配中文  
  70.      */  
  71.     @Test  
  72.     public void testCheckChinese() {  
  73.         boolean result = RegexUtils.checkChinese("中文");  
  74.         Assert.assertTrue(result);  
  75.     }  
  76.       
  77.     /**  
  78.      * 验证日期  
  79.      */  
  80.     @Test  
  81.     public void testCheckBirthday() {  
  82.         boolean result = RegexUtils.checkBirthday("1992/09/03");  
  83.         Assert.assertTrue(result);  
  84.     }  
  85.       
  86.     /**  
  87.      * 验证中国邮政编码  
  88.      */  
  89.     @Test  
  90.     public void testCheckPostcode() {  
  91.         boolean result = RegexUtils.checkPostcode("417100");  
  92.         Assert.assertTrue(result);  
  93.     }  
  94.       
  95.     /**  
  96.      * 验证URL地址  
  97.      */  
  98.     @Test  
  99.     public  void testCheckURL() {  
  100.         boolean result = RegexUtils.checkURL("http://blog.csdn.com:80/xyang81/article/details?name=&abc=中文");  
  101.         Assert.assertTrue(result);  
  102.     }  
  103.       
  104.     /**  
  105.      * 验证IP地址  
  106.      */  
  107.     @Test  
  108.     public void testCheckIpAddress() {  
  109.         boolean result = RegexUtils.checkIpAddress("192.1.22.255");  
  110.         Assert.assertTrue(result);  
  111.     }  
  112. }  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值