RegexUtils.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package jnpf.util;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. /**
  5. *
  6. * @author JNPF开发平台组
  7. * @version V3.1.0
  8. * @copyright 引迈信息技术有限公司
  9. * @date 2021/3/16 10:55
  10. */
  11. public class RegexUtils {
  12. /**
  13. * 验证Email
  14. * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
  15. * @return 验证成功返回true,验证失败返回false
  16. */
  17. public static boolean checkEmail(String email) {
  18. String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
  19. return Pattern.matches(regex, email);
  20. }
  21. /**
  22. * 验证身份证号码
  23. * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
  24. * @return 验证成功返回true,验证失败返回false
  25. */
  26. public static boolean checkIdCard(String idCard) {
  27. String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
  28. return Pattern.matches(regex,idCard);
  29. }
  30. /**
  31. * 验证手机号码(支持国际格式,+86135xxxx...(中国内地),+00852137xxxx...(中国香港))
  32. * @param mobile 移动、联通、电信运营商的号码段
  33. *<p>移动的号段:134(0-8)、135、136、137、138、139、147(预计用于TD上网卡)
  34. *、150、151、152、157(TD专用)、158、159、187(未启用)、188(TD专用)</p>
  35. *<p>联通的号段:130、131、132、155、156(世界风专用)、185(未启用)、186(3g)</p>
  36. *<p>电信的号段:133、153、180(未启用)、189</p>
  37. * @return 验证成功返回true,验证失败返回false
  38. */
  39. public static boolean checkMobile(String mobile) {
  40. String regex = "(\\+\\d+)?1[345789]\\d{9}$";
  41. return Pattern.matches(regex,mobile);
  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}\\-?)?\\d{7,8}$";
  55. return Pattern.matches(regex, phone);
  56. }
  57. /**
  58. * 验证整数(正整数和负整数)
  59. * @param digit 一位或多位0-9之间的整数
  60. * @return 验证成功返回true,验证失败返回false
  61. */
  62. public static boolean checkDigit(String digit) {
  63. String regex = "\\-?[1-9]\\d+";
  64. return Pattern.matches(regex,digit);
  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. * @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
  78. * @return 验证成功返回true,验证失败返回false
  79. */
  80. public static boolean checkBlankSpace(String blankSpace) {
  81. String regex = "\\s+";
  82. return Pattern.matches(regex,blankSpace);
  83. }
  84. /**
  85. * 验证中文
  86. * @param chinese 中文字符
  87. * @return 验证成功返回true,验证失败返回false
  88. */
  89. public static boolean checkChinese(String chinese) {
  90. String regex = "^[\u4E00-\u9FA5]+$";
  91. return Pattern.matches(regex,chinese);
  92. }
  93. /**
  94. * 验证日期(年月日)
  95. * @param birthday 日期,格式:1992-09-03,或1992.09.03
  96. * @return 验证成功返回true,验证失败返回false
  97. */
  98. public static boolean checkBirthday(String birthday) {
  99. String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
  100. return Pattern.matches(regex,birthday);
  101. }
  102. /**
  103. * 验证URL地址
  104. * @param url 格式:http://blog.csdn.net:80/xyang81/article/details/7705960? 或 http://www.csdn.net:80
  105. * @return 验证成功返回true,验证失败返回false
  106. */
  107. public static boolean checkUrl(String url) {
  108. String regex = "(https?://(w{3}\\.)?)?\\w+\\.\\w+(\\.[a-zA-Z]+)*(:\\d{1,5})?(/\\w*)*(\\??(.+=.*)?(&.+=.*)?)?";
  109. return Pattern.matches(regex, url);
  110. }
  111. /**
  112. * <pre>
  113. * 获取网址 URL 的一级域
  114. * </pre>
  115. *
  116. * @param url
  117. * @return
  118. */
  119. public static String getDomain(String url) {
  120. String regex = "(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)";
  121. Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  122. // 获取完整的域名
  123. Matcher matcher = p.matcher(url);
  124. matcher.find();
  125. return matcher.group();
  126. }
  127. /**
  128. * 匹配中国邮政编码
  129. * @param postcode 邮政编码
  130. * @return 验证成功返回true,验证失败返回false
  131. */
  132. public static boolean checkPostcode(String postcode) {
  133. String regex = "[1-9]\\d{5}";
  134. return Pattern.matches(regex, postcode);
  135. }
  136. /**
  137. * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小)
  138. * @param ipAddress IPv4标准地址
  139. * @return 验证成功返回true,验证失败返回false
  140. */
  141. public static boolean checkIpAddress(String ipAddress) {
  142. 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})?))";
  143. return Pattern.matches(regex, ipAddress);
  144. }
  145. /**
  146. * 验证整数和浮点数(正整数和正两位小数)
  147. * @param decimals 整数或多位0-9之间的浮点数,如:1.23,233.30
  148. * @return 验证成功返回true,验证失败返回false
  149. */
  150. public static boolean checkDecimals2(String decimals) {
  151. String regex = "^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$";
  152. return Pattern.matches(regex,decimals);
  153. }
  154. /**
  155. * 验证是否为正整数
  156. * @param digit 一位或多位0-9之间的整数
  157. * @return 验证成功返回true,验证失败返回false
  158. */
  159. public static boolean checkDigit2(String digit) {
  160. String regex = "[1-9]\\d*";
  161. return Pattern.matches(regex,digit);
  162. }
  163. /**
  164. * 验证请假时间为0.5的倍数
  165. * @param leave
  166. * @return
  167. */
  168. public static boolean checkLeave(String leave){
  169. String regex = "^[1-9]\\d*\\.[5]$|0\\.[5]$||\\d\\.0|^[1-9]\\d*$";
  170. return Pattern.matches(regex,leave);
  171. }
  172. /**
  173. * 编码正则
  174. * 只能输入英文、数字和小数点且小数点不能放在首尾
  175. * @return
  176. */
  177. public static boolean checkEnCode(String enCode){
  178. String regex = "^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9.]*[a-zA-Z0-9]$";
  179. return Pattern.matches(regex,enCode);
  180. }
  181. /**
  182. * 编码正则(翻译标记用)
  183. * 只能输入字母、数字、点、横线和下划线,且以字母开头
  184. * @return
  185. */
  186. public static boolean checkEnCode2(String enCode){
  187. String regex = "^[a-zA-Z][a-zA-Z0-9._-]*$";
  188. return Pattern.matches(regex,enCode);
  189. }
  190. /**
  191. * 名称和编码等使用
  192. * xxx不能含有特殊符号
  193. * @return
  194. */
  195. public static boolean checkSpecoalSymbols(String enCode){
  196. String regex = "^([\u4e00-\u9fa5]|[a-zA-Z0-9])+$";
  197. return Pattern.matches(regex,enCode);
  198. }
  199. /**
  200. * 验证只能由字母、数字、下划线组成,且不能以数字开头
  201. * 用于数据库表名,字段名
  202. *
  203. * @return
  204. */
  205. public static boolean checkName(String name) {
  206. String regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
  207. return Pattern.matches(regex, name);
  208. }
  209. }