正则表达式验证工具类RegexUtils.java

前端之家收集整理的这篇文章主要介绍了正则表达式验证工具类RegexUtils.java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文:Java常用正则表达式验证工具类RegexUtils.java

代码下载地址:http://www.zuidaima.com/share/1550463379442688.htm

Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合。

1. 电话号码

2. 邮编

3. QQ

4. E-mail

5. 手机号码

6. URL

7. 是否为数字

8. 是否为中文

9. 身份证

10. 域名

11. IP 。。。。

常用验证应有尽有! 这的确是您从事 web 开发,服务器端表单验证之良品!你,值得拥有 ^_^

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

猜你在找的正则表达式相关文章