1. public class RegxUtils {
  2. //------------------常量定义
  3. /**
  4. * Email正则表达式="^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
  5. */
  6. //public static final String EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";;
  7. public static final String EMAIL = "\\w+(\\.\\w+)*@\\w+(\\.\\w+)+";
  8. /**
  9. * 电话号码正则表达式= (^(\d{2,4}[-_-—]?)?\d{3,8}([-_-—]?\d{3,8})?([-_-—]?\d{1,7})?$)|(^0?1[35]\d{9}$)
  10. */
  11. public static final String PHONE = "(^(\\d{2,4}[-_-—]?)?\\d{3,8}([-_-—]?\\d{3,8})?([-_-—]?\\d{1,7})?$)|(^0?1[35]\\d{9}$)";
  12. /**
  13. * 手机号码正则表达式=^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$
  14. */
  15. public static final String MOBILE = "^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\\d{8}$";
  16.  
  17. /**
  18. * Integer正则表达式 ^-?(([1-9]\d*$)|0)
  19. */
  20. public static final String INTEGER = "^-?(([1-9]\\d*$)|0)";
  21. /**
  22. * 正整数正则表达式 >=0 ^[1-9]\d*|0$
  23. */
  24. public static final String INTEGER_NEGATIVE = "^[1-9]\\d*|0$";
  25. /**
  26. * 负整数正则表达式 <=0 ^-[1-9]\d*|0$
  27. */
  28. public static final String INTEGER_POSITIVE = "^-[1-9]\\d*|0$";
  29. /**
  30. * Double正则表达式 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
  31. */
  32. public static final String DOUBLE = "^-?([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0)$";
  33. /**
  34. * 正Double正则表达式 >=0 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
  35. */
  36. public static final String DOUBLE_NEGATIVE = "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*|0?\\.0+|0$";
  37. /**
  38. * 负Double正则表达式 <= 0 ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
  39. */
  40. public static final String DOUBLE_POSITIVE = "^(-([1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*))|0?\\.0+|0$";
  41. /**
  42. * 年龄正则表达式 ^(?:[1-9][0-9]?|1[01][0-9]|120)$ 匹配0-120岁
  43. */
  44. public static final String AGE = "^(?:[1-9][0-9]?|1[01][0-9]|120)$";
  45. /**
  46. * 邮编正则表达式 [0-9]\d{5}(?!\d) 国内6位邮编
  47. */
  48. public static final String CODE = "[0-9]\\d{5}(?!\\d)";
  49. /**
  50. * 匹配由数字、26个英文字母或者下划线组成的字符串 ^\w+$
  51. */
  52. public static final String STR_ENG_NUM_ = "^\\w+$";
  53. /**
  54. * 匹配由数字和26个英文字母组成的字符串 ^[A-Za-z0-9]+$
  55. */
  56. public static final String STR_ENG_NUM = "^[A-Za-z0-9]+";
  57. /**
  58. * 匹配由26个英文字母组成的字符串 ^[A-Za-z]+$
  59. */
  60. public static final String STR_ENG = "^[A-Za-z]+$";
  61. /**
  62. * 过滤特殊字符串正则
  63. * regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
  64. */
  65. public static final String STR_SPECIAL = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
  66. /***
  67. * 日期正则 支持:
  68. * YYYY-MM-DD
  69. * YYYY/MM/DD
  70. * YYYY_MM_DD
  71. * YYYYMMDD
  72. * YYYY.MM.DD的形式
  73. */
  74. public static final String DATE_ALL = "((^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(10|12|0?[13578])([-\\/\\._]?)(3[01]|[12][0-9]|0?[1-9])$)" +
  75. "|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(11|0?[469])([-\\/\\._]?)(30|[12][0-9]|0?[1-9])$)" +
  76. "|(^((1[8-9]\\d{2})|([2-9]\\d{3}))([-\\/\\._]?)(0?2)([-\\/\\._]?)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([3579][26]00)" +
  77. "([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)" +
  78. "|(^([1][89][0][48])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][0][48])([-\\/\\._]?)" +
  79. "(0?2)([-\\/\\._]?)(29)$)" +
  80. "|(^([1][89][2468][048])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|(^([2-9][0-9][2468][048])([-\\/\\._]?)(0?2)" +
  81. "([-\\/\\._]?)(29)$)|(^([1][89][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$)|" +
  82. "(^([2-9][0-9][13579][26])([-\\/\\._]?)(0?2)([-\\/\\._]?)(29)$))";
  83. /***
  84. * 日期正则 支持:
  85. * YYYY-MM-DD
  86. */
  87. public static final String DATE_FORMAT1 = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
  88.  
  89. /**
  90. * URL正则表达式
  91. * 匹配 http www ftp
  92. */
  93. public static final String URL = "^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?" +
  94. "(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*" +
  95. "(\\w*:)*(\\w*\\+)*(\\w*\\.)*" +
  96. "(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$";
  97.  
  98. /**
  99. * 身份证正则表达式
  100. */
  101. public static final String IDCARD = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})" +
  102. "(([1|2][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}" +
  103. "[Xx0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
  104.  
  105. /**
  106. * 机构代码
  107. */
  108. public static final String JIGOU_CODE = "^[A-Z0-9]{8}-[A-Z0-9]$";
  109.  
  110. /**
  111. * 匹配数字组成的字符串 ^[0-9]+$
  112. */
  113. public static final String STR_NUM = "^[0-9]+$";
  114.  
  115. ////------------------验证方法
  116.  
  117. /**
  118. * 判断字段是否为空 符合返回ture
  119. *
  120. * @param str
  121. * @return boolean
  122. */
  123. public static synchronized boolean StrisNull(String str) {
  124. return null == str || str.trim().length() <= 0 ? true : false;
  125. }
  126.  
  127. /**
  128. * 判断字段是非空 符合返回ture
  129. *
  130. * @param str
  131. * @return boolean
  132. */
  133. public static boolean StrNotNull(String str) {
  134. return !StrisNull(str);
  135. }
  136.  
  137. /**
  138. * 字符串null转空
  139. *
  140. * @param str
  141. * @return boolean
  142. */
  143. public static String nulltoStr(String str) {
  144. return StrisNull(str) ? "" : str;
  145. }
  146.  
  147. /**
  148. * 字符串null赋值默认值
  149. *
  150. * @param str 目标字符串
  151. * @param defaut 默认值
  152. * @return String
  153. */
  154. public static String nulltoStr(String str, String defaut) {
  155. return StrisNull(str) ? defaut : str;
  156. }
  157.  
  158. /**
  159. * 判断字段是否为Email 符合返回ture
  160. *
  161. * @param str
  162. * @return boolean
  163. */
  164. public static boolean isEmail(String str) {
  165. return Regular(str, EMAIL);
  166. }
  167.  
  168. /**
  169. * 判断是否为电话号码 符合返回ture
  170. *
  171. * @param str
  172. * @return boolean
  173. */
  174. public static boolean isPhone(String str) {
  175. return Regular(str, PHONE);
  176. }
  177.  
  178. /**
  179. * 判断是否为手机号码 符合返回ture
  180. *
  181. * @param str
  182. * @return boolean
  183. */
  184. public static boolean isMobile(String str) {
  185. return Regular(str, MOBILE);
  186. }
  187.  
  188. /**
  189. * 判断是否为Url 符合返回ture
  190. *
  191. * @param str
  192. * @return boolean
  193. */
  194. public static boolean isUrl(String str) {
  195. return Regular(str, URL);
  196. }
  197.  
  198. /**
  199. * 判断字段是否为数字 正负整数 正负浮点数 符合返回ture
  200. *
  201. * @param str
  202. * @return boolean
  203. */
  204. public static boolean isNumber(String str) {
  205. return Regular(str, DOUBLE);
  206. }
  207.  
  208. /**
  209. * 判断字段是否为INTEGER 符合返回ture
  210. *
  211. * @param str
  212. * @return boolean
  213. */
  214. public static boolean isInteger(String str) {
  215. return Regular(str, INTEGER);
  216. }
  217.  
  218. /**
  219. * 判断字段是否为正整数正则表达式 >=0 符合返回ture
  220. *
  221. * @param str
  222. * @return boolean
  223. */
  224. public static boolean isINTEGER_NEGATIVE(String str) {
  225. return Regular(str, INTEGER_NEGATIVE);
  226. }
  227.  
  228. /**
  229. * 判断字段是否为负整数正则表达式 <=0 符合返回ture
  230. *
  231. * @param str
  232. * @return boolean
  233. */
  234. public static boolean isINTEGER_POSITIVE(String str) {
  235. return Regular(str, INTEGER_POSITIVE);
  236. }
  237.  
  238. /**
  239. * 判断字段是否为DOUBLE 符合返回ture
  240. *
  241. * @param str
  242. * @return boolean
  243. */
  244. public static boolean isDouble(String str) {
  245. return Regular(str, DOUBLE);
  246. }
  247.  
  248. /**
  249. * 判断字段是否为正浮点数正则表达式 >=0 符合返回ture
  250. *
  251. * @param str
  252. * @return boolean
  253. */
  254. public static boolean isDOUBLE_NEGATIVE(String str) {
  255. return Regular(str, DOUBLE_NEGATIVE);
  256. }
  257.  
  258. /**
  259. * 判断字段是否为负浮点数正则表达式 <=0 符合返回ture
  260. *
  261. * @param str
  262. * @return boolean
  263. */
  264. public static boolean isDOUBLE_POSITIVE(String str) {
  265. return Regular(str, DOUBLE_POSITIVE);
  266. }
  267.  
  268. /**
  269. * 判断字段是否为日期 符合返回ture
  270. *
  271. * @param str
  272. * @return boolean
  273. */
  274. public static boolean isDate(String str) {
  275. return Regular(str, DATE_ALL);
  276. }
  277.  
  278. /**
  279. * 验证2010-12-10
  280. *
  281. * @param str
  282. * @return
  283. */
  284. public static boolean isDate1(String str) {
  285. return Regular(str, DATE_FORMAT1);
  286. }
  287.  
  288. /**
  289. * 判断字段是否为年龄 符合返回ture
  290. *
  291. * @param str
  292. * @return boolean
  293. */
  294. public static boolean isAge(String str) {
  295. return Regular(str, AGE);
  296. }
  297.  
  298. /**
  299. * 判断字段是否超长
  300. * 字串为空返回fasle, 超过长度{leng}返回ture 反之返回false
  301. *
  302. * @param str
  303. * @param leng
  304. * @return boolean
  305. */
  306. public static boolean isLengOut(String str, int leng) {
  307. return StrisNull(str) ? false : str.trim().length() > leng;
  308. }
  309.  
  310. /**
  311. * 判断字段是否为身份证 符合返回ture
  312. *
  313. * @param str
  314. * @return boolean
  315. */
  316. public static boolean isIdCard(String str) {
  317. if (StrisNull(str)) {
  318. return false;
  319. }
  320. if (str.trim().length() == 15 || str.trim().length() == 18) {
  321. return Regular(str, IDCARD);
  322. } else {
  323. return false;
  324. }
  325.  
  326. }
  327.  
  328. /**
  329. * 判断字段是否为邮编 符合返回ture
  330. *
  331. * @param str
  332. * @return boolean
  333. */
  334. public static boolean isCode(String str) {
  335. return Regular(str, CODE);
  336. }
  337.  
  338. /**
  339. * 判断字符串是不是全部是英文字母
  340. *
  341. * @param str
  342. * @return boolean
  343. */
  344. public static boolean isEnglish(String str) {
  345. return Regular(str, STR_ENG);
  346. }
  347.  
  348. /**
  349. * 判断字符串是不是全部是英文字母+数字
  350. *
  351. * @param str
  352. * @return boolean
  353. */
  354. public static boolean isENG_NUM(String str) {
  355. return Regular(str, STR_ENG_NUM);
  356. }
  357.  
  358. /**
  359. * 判断字符串是不是全部是英文字母+数字+下划线
  360. *
  361. * @param str
  362. * @return boolean
  363. */
  364. public static boolean isENG_NUM_(String str) {
  365. return Regular(str, STR_ENG_NUM_);
  366. }
  367.  
  368. /**
  369. * 过滤特殊字符串 返回过滤后的字符串
  370. *
  371. * @param str
  372. * @return boolean
  373. */
  374. public static String filterStr(String str) {
  375. Pattern p = Pattern.compile(STR_SPECIAL);
  376. Matcher m = p.matcher(str);
  377. return m.replaceAll("").trim();
  378. }
  379.  
  380. /**
  381. * 校验机构代码格式
  382. *
  383. * @return
  384. */
  385. public static boolean isJigouCode(String str) {
  386. return Regular(str, JIGOU_CODE);
  387. }
  388.  
  389. /**
  390. * 判断字符串是不是数字组成
  391. *
  392. * @param str
  393. * @return boolean
  394. */
  395. public static boolean isSTR_NUM(String str) {
  396. return Regular(str, STR_NUM);
  397. }
  398.  
  399. /**
  400. * 匹配是否符合正则表达式pattern 匹配返回true
  401. *
  402. * @param str 匹配的字符串
  403. * @param pattern 匹配模式
  404. * @return boolean
  405. */
  406. private static boolean Regular(String str, String pattern) {
  407. if (null == str || str.trim().length() <= 0)
  408. return false;
  409. Pattern p = Pattern.compile(pattern);
  410. Matcher m = p.matcher(str);
  411. return m.matches();
  412. }
  413.  
  414. public static void main(String[] args) {
  415. boolean age = RegxUtils.isAge("100");
  416. System.out.println(age);
  417. }
  418. }

RegxUtils正则表达式工具类的更多相关文章

  1. JAVA自动生成正则表达式工具类

    经过很久的努力,终于完成了JAVA自动生成正则表达式工具类.还记得之前需要正则,老是从网上找吗?找了想修改也不会修改.现在不用再为此烦恼了,使用此生成类轻松搞定所有正则表达式.赶快在同事面前炫一下吧. ...

  2. 正则表达式工具类,正则表达式封装,Java正则表达式

    正则表达式工具类 正则表达式封装 Java正则表达式 >>>>>>>>>>>>>>>>>>& ...

  3. [Swift]正则表达式工具类

    正则表达式工具类 import Foundation //基于NSRegularExpression api 的正则处理工具类 public struct Regex { private let re ...

  4. Android 中正则表达式工具类

    package com.example.administrator.magiclamp.utils; import java.util.regex.Pattern; /** * 校验器:利用正则表达式 ...

  5. Java常用工具类之RegexpUtils,正则表达式工具类

    package com.test.core.util; import org.apache.log4j.Logger; import org.apache.oro.text.regex.Malform ...

  6. Android常用正则工具类

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

  7. 实用工具类--第三方开源--Lazy

    下载地址 :https://github.com/ddwhan0123/Lazy 工具 描述 AnimationUtils 动画工具类 AppUtils APP相关信息工具类 AssetDatabas ...

  8. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

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

    Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合. 1. 电话号码 2. 邮编 3. QQ 4. E-mail 5. 手机号码 6. URL 7. 是否为数字 8. 是否为中文 9. ...

随机推荐

  1. 【转】HTTPS建立连接的过程

    原文链接:https://www.cnblogs.com/shiqi17/p/9756880.html https://www.jianshu.com/p/bd75ab32ae57 HTTP建立连接的 ...

  2. 用一个例子说明oracle临时表,创建过程,

    --创建临时表,规定好格式,是必须的,不同于sqlserver那么随意: Create Global Temporary Table record4 (   yljgdm VARCHAR2(22) n ...

  3. 2019年最新50道java基础部分面试题

    [软帝学院]1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语法,集合的语法,io 的语法,虚拟机方面的语法. 1.一个".j ...

  4. 日志检索实战 grep sed

    日志检索实战 grep sed 参考 sed命令 使用 grep -5 'parttern' inputfile //打印匹配行的前后5行 grep -C 5 'parttern' inputfile ...

  5. jQuery中的文档处理(五)

    1. append(content|fn), 向每个匹配的元素内部追加内容 在内部结尾添加. 参数说明: content:String, Element, jQuery,要追加到目标中的内容 func ...

  6. luoguP4588 [TJOI2018]数学计算

    题意 考虑一个操作会对一段时间内的询问产生影响,于是将线段树上的这段时间打上乘这个数的标记,最后dfs整颗线段树. code: #include<bits/stdc++.h> using ...

  7. Nginx 负载均衡算法

    Nginx 负载均衡算法 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务,如果后端某台服务器死机,自动剔除故障系统,使用户访问不受影响. upstream tomcat_server { ...

  8. 使用system V实现读者写者问题

    #include <stdio.h> #include <sys/sem.h> #include <sys/ipc.h> #include <string.h ...

  9. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) D. Power Products 数学 暴力

    D. Power Products You are given n positive integers a1,-,an, and an integer k≥2. Count the number of ...

  10. python进阶之命名空间与作用域

    命名空间 Namespace Namespace命名空间,命名空间是一个字典(dictionary),它的键就是变量名,它的值就是那些变量的值.命名空间的一大作用是避免名字冲突. python使用命名 ...