此类提供日常开发中经常使用的正则验证函数。比方:邮箱、手机号、电话号码、身份证号码、日期、数字、小数、URL、IP地址等。使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:

        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(input);

        return m.matches();

每一个正则可能还有待优化的地方,您如有更好的方式实现某一个功能的验证,欢迎提出来大家一起讨论。

以下是工具类的完整代码:

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

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

android经常使用正则工具类的更多相关文章

  1. Android常用正则工具类

    此类提供日常开发中常用的正则验证函数,比如:邮箱.手机号.电话号码.身份证号码.日期.数字.小数.URL.IP地址等.使用Pattern对象的matches方法进行整个字符匹配,调用该方法相当于:   ...

  2. Android开发调试日志工具类[支持保存到SD卡]

    直接上代码: package com.example.callstatus; import java.io.File; import java.io.FileWriter; import java.i ...

  3. Android开源项目大全 - 工具类

    主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...

  4. wemall app商城源码android开发MD5加密工具类

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供 ...

  5. Android弹出Toast工具类总结

    Android弹出Toast工具类总结,包括系统自带的,也包括自定义的. public class ToastUtil { public ToastUtil() { } public static T ...

  6. Android 中替代 sharedpreferences 工具类的实现

    Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...

  7. 正则工具类以及FinalClass

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/27811103 完整版见https://jadyer. ...

  8. 获取 Android APP 版本信息工具类(转载)

    获取 Android APP 版本信息工具类 获取手机APP版本信息工具类 1.获取版本名称 2.获取版本号 3.获取App的名称 package com.mingyue.nanshuibeidiao ...

  9. Android 获取手机的厂商、型号、Android系统版本号等工具类(转载)

    Android 获取手机的厂商.型号.Android系统版本号等工具类 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android系统版本号 5.获取手机IMEI串 ...

随机推荐

  1. Head First HTML5 Programming笔记--chapter2 介绍Javascript和DOM

    你已经了解了HTML标记(也称为结构),而且知道了CSS样式(也称为表示),剩下的就是Javascript(也称为行为). JavaScript的工作方式 1. 编写 你创建HTML标记和JavaSc ...

  2. 【0门槛】PR稿的自我修养

    本文来自网易云社区 作者:巩爽 十一过完,离2018年结束就只剩下85天啦!是不是2016年许下的2017年的梦想,在2018年还没有实现? 做过的项目仿佛都小有成就,可惜只是内部自嗨,想做域外宣传却 ...

  3. IOS 自动布局-UIStackPanel和UIGridPanel(一)

    我以前是做windows phone开发的,后来转做IOS的开发,因此很多windows phone上面的开发经验也被我带到了IOS中.其实有些经验本身跟平台无关,跟平台有关的无非就是实现方法而已.好 ...

  4. D. Frequent values

    D. Frequent values Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 131072KB   64-bit intege ...

  5. MHA脚本master_ip_failover.pl(三)

    #!/usr/bin/env perl use strict;use warnings FATAL => 'all'; use Getopt::Long; my ( $command, $ssh ...

  6. URAL Formula 1 ——插头DP

    [题目分析] 一直听说这是插头DP入门题目. 难到爆炸. 写了2h,各种大常数,ural垫底. [代码] #include <cstdio> #include <cstring> ...

  7. Struts2的验证框架简单吗?

    Struts2验证框架是基于Struts拦截器开发的,具有良好的扩展性:一般的验证都可以支持.现在我们以一个注册验证的例子进行总结: 新建一个model,User: public class User ...

  8. noip2013华容道

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

  9. VS2015 当前不会命中断点,还没有为该文档加载任何符号

    这种小问题,我想只需要清理解决方案重新生成就好啦,结果...2个小时过去后.. 最后问了昨天做过修改的同事,修改了什么.. 设置成生成调试信息,仅以此文纪念我那逝去的青春

  10. 如何稳定地使用 Google 搜索https://encrypted.google.com/

    方法很简单.用记事本打开 hosts 文件(Windows Vista 和 Windows 7 用户请先使用管理员权限打开记事本,然后将 hosts 文件拖进记事本中),在最下面添加如下内容: 203 ...