1、正则表达式验证-工具类

  1. @Slf4j
  2. public class RegValidateUtils {
  3. //匹配标准通配符
  4. private static final String REGEXP = "\\$\\{\\w{0,}[-+]{0,}\\d{0,}\\}";
  5. /**
  6. * 解析字符串中yyyymmdd格式内容
  7. */
  8. private static final String PARSE_DATE = "[1-9]\\d{3}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])";
  9. /**
  10. * 匹配yyyymmdd格式
  11. */
  12. private static final String MATCHER_DATE = "^[2]\\d{3}[0|1][0-9][0-3]\\d$";
  13. //标准日期格式yyyymmdd|yyyymm
  14. private static final String STANDARDDATE = "^yyyymmdd$|^yyyymm$";
  15. //匹配日期格式串yyyymm-+n/yyyymmdd-+n
  16. private static final String DATAPARTERN = "^yyyymmdd[-+]\\d{1}$|^yyyymm[-+]\\d{1}$";
  17. //匹配周时间格式,如:201911(2019年第11周)
  18. private static final String WEEK = "^week$";
  19. //匹配版本和adcode
  20. private static final String DATAVERSION = "^data_version$";
  21. private static final String ADCODE = "^adcode$";
  22. /**
  23. * 获取字符串中数字
  24. */
  25. private static final String NUMBER ="[^0-9]";
  26. private static final String ISNUMBER = "^-?\\d+(\\.\\d+)?$";
  27. /**
  28. * 获取字符串中数字英文字母
  29. */
  30. private static final String NUMBERLETTER ="[^a-zA-Z0-9]";
  31. //手机号码
  32. public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
  33. //邮箱
  34. public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
  35. //url
  36. public static final String REGEX_URL = "http(s)?://.*";
  37. //日期
  38. public static final String REGEX_SIMPLE_DATE1 = "\\d{4}-\\d{2}-\\d{2}";
  39. /**
  40. * 字符串匹配
  41. * @param regex
  42. * @param str
  43. * @return
  44. */
  45. public static boolean match(String regex, String str) {
  46. Pattern pattern = Pattern.compile(regex);
  47. Matcher matcher = pattern.matcher(str);
  48. return matcher.find();
  49. }
  50. /**
  51. * 判断通配符中是否存在非法字符
  52. * @param content
  53. * @return
  54. */
  55. public static boolean validateLegalStr(String content) {
  56. Pattern r = Pattern.compile(REGEXP);
  57. Matcher m = r.matcher(content);
  58. while(m.find()) {
  59. if (!isDateFormat(m.group().replace("${","}").replace("}",""))){
  60. SQLParserUtils.catchError("通配符不合法");
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. /**
  67. * 校验通配符中字符格式是否为准确的日期格式
  68. * @param content
  69. */
  70. public static boolean isDateFormat(String content){
  71. if (match(STANDARDDATE,content)){
  72. return true;
  73. }
  74. if (match(DATAPARTERN,content)){
  75. return true;
  76. }
  77. if (match(DATAVERSION,content)){
  78. return true;
  79. }
  80. if (match(ADCODE,content)){
  81. return true;
  82. }
  83. if (match(WEEK,content)) {
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * 解析指标ID
  90. * @param exp
  91. * @return
  92. */
  93. public static List<String> getExpParse(String exp) {
  94. if (Objects.isNull(exp)){
  95. return null;
  96. }
  97. Pattern r = Pattern.compile(REGEXP);
  98. Matcher m = r.matcher(exp);
  99. List<String> indexs = new ArrayList<>();
  100. while (m.find()) {
  101. indexs.add(m.group().replace("${","}").replace("}",""));
  102. }
  103. return indexs;
  104. }
  105. /**
  106. * 解析字符串中yyyymmdd格式内容
  107. * @param date
  108. * @return
  109. */
  110. public static String getDateParse(String date) {
  111. if (Objects.isNull(date)){
  112. return "";
  113. }
  114. Pattern r = Pattern.compile(PARSE_DATE);
  115. Matcher m = r.matcher(date);
  116. String indexs = "";
  117. while (m.find()) {
  118. indexs = m.group();
  119. }
  120. return indexs;
  121. }
  122. /**
  123. * 获取解析出来当前天的日期
  124. * @param date
  125. * @return
  126. */
  127. public static String getCurDateParse(String date) {
  128. if (Objects.isNull(date)){
  129. return "";
  130. }
  131. Pattern r = Pattern.compile(PARSE_DATE);
  132. Matcher m = r.matcher(date);
  133. String curDate = DateUtils.getDate("yyyyMMdd");
  134. while (m.find()) {
  135. String indexs = m.group();
  136. //判断通过正则表达式解析出的日期和当前日期相等
  137. if(curDate.equals(indexs)){
  138. return indexs;
  139. }
  140. }
  141. return "";
  142. }
  143. /**
  144. * yyyyMMdd格式校验
  145. * @param date
  146. * @return
  147. */
  148. public static boolean checkDateStyle(String date){
  149. boolean flag = false;
  150. Pattern r = Pattern.compile(MATCHER_DATE);
  151. Matcher m = r.matcher(date);
  152. if(m.matches()){
  153. flag = true;
  154. }
  155. return flag;
  156. }
  157. /**
  158. * 获取字符串中数字
  159. * @param str
  160. * @return
  161. */
  162. public static int getStrNumber(String str){
  163. Pattern r = Pattern.compile(NUMBER);
  164. Matcher m = r.matcher(str);
  165. if(isNumber(m.replaceAll("").trim())){
  166. return Integer.valueOf(m.replaceAll("").trim());
  167. }
  168. return 0;
  169. }
  170. /**
  171. * 判断是否数字
  172. * @param str
  173. * @return
  174. */
  175. public static boolean isNumber(String str){
  176. if (str == null) {
  177. return false;
  178. }
  179. Pattern pattern = Pattern.compile(ISNUMBER);
  180. return pattern.matcher(str).matches();
  181. }
  182. /**
  183. * 获取字符串中数字英文字母
  184. * @param str
  185. * @return
  186. */
  187. public static String getStrNumberLetter(String str){
  188. Pattern r = Pattern.compile(NUMBERLETTER);
  189. Matcher m = r.matcher(str);
  190. String numberLetter = m.replaceAll("").trim();
  191. return numberLetter;
  192. }
  193. /**
  194. * 提取全局参数中时间参数
  195. * @param params
  196. * @return
  197. */
  198. public static String getDateParam(String params) {
  199. if (StringUtils.isEmpty(params)) {
  200. return null;
  201. }
  202. String reg = "\\r|\\n|\\\\s";
  203. String str = params.replaceAll(reg, "");
  204. String[] split = str.toLowerCase().split("and");
  205. for (String param : split) {
  206. if (param.contains("yyyymm")) {
  207. return param;
  208. }
  209. if (param.contains("yyyymmdd")) {
  210. return param;
  211. }
  212. if (param.contains("week")) {
  213. return param;
  214. }
  215. }
  216. return params;
  217. }
  218. /**
  219. * @Param: [email]
  220. * @Desc: 校验是否是邮箱格式
  221. **/
  222. public static boolean isEmail(String email) {
  223. return Pattern.matches(REGEX_EMAIL, email);
  224. }
  225. /**
  226. * @Param: 校验是否是url格式
  227. * @Desc:
  228. **/
  229. public static boolean isUrl(String url) {
  230. if(StringUtils.isEmpty(url)){
  231. return false;
  232. }
  233. return Pattern.matches(REGEX_URL, url);
  234. }
  235. /**
  236. * @Param: 校验是否是手机号
  237. * @Desc:
  238. **/
  239. public static boolean isMobile(String mobileNum){
  240. return Pattern.matches(REGEX_MOBILE,mobileNum);
  241. }
  242. public static boolean isDateStr1(String dateStr){
  243. return Pattern.matches(REGEX_SIMPLE_DATE1,dateStr);
  244. }
  245. }