From 135ffd2fc299ddc0cb14fd3e2b6dde0e24f2e6b9 Mon Sep 17 00:00:00 2001 From: zhangjinli Date: Wed, 6 Jul 2022 16:51:46 +0800 Subject: [PATCH] zh --- .../controller/UtilsController.java | 14 +- .../controller/wxApp/VideoUController.java | 3 +- .../com/zh/project0512/utils/RegexUtil.java | 2 +- .../com/zh/project0512/utils/RegexUtils.java | 153 ++++++++++++++++++ 4 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/zh/project0512/utils/RegexUtils.java diff --git a/src/main/java/com/zh/project0512/controller/UtilsController.java b/src/main/java/com/zh/project0512/controller/UtilsController.java index 0ae30eb..1b10246 100644 --- a/src/main/java/com/zh/project0512/controller/UtilsController.java +++ b/src/main/java/com/zh/project0512/controller/UtilsController.java @@ -9,6 +9,7 @@ import com.zh.project0512.service.ITaskService; import com.zh.project0512.service.IVideoService; import com.zh.project0512.utils.FileTypeUtil; import com.zh.project0512.utils.RegexUtil; +import com.zh.project0512.utils.RegexUtils; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.swagger.v3.oas.annotations.Operation; @@ -96,9 +97,9 @@ public class UtilsController { commpressPicCycle(filePathSmall, picSizeLimit, 0.8); res.put("fileUrlSmall", "/upload/" + newFileNameSmall); } -// if (!dest.getParentFile().exists()) { -// dest.getParentFile().mkdirs(); -// } + if (!dest.getParentFile().exists()) { + dest.getParentFile().mkdirs(); + } file.transferTo(dest); res.put("fileUrl", "/upload/" + newFileName); @@ -199,11 +200,8 @@ public class UtilsController { } public static void main(String[] args) { - String url = "4.38 wfb:/ 复制打开抖音,看看【酸味真火的作品】坚持总会有收获的# 意想不到的情侣健身 https://v.douyin.com/YT982Sf/"; - Pattern p = Pattern.compile("((https://)+\\S*(/)\\S*(/))", Pattern.CASE_INSENSITIVE); - System.out.println(new RegexUtil().httpReg(url)); -// Matcher m = p.matcher(url); -// System.out.println(m); + String url = "4.38 wfb:/ 复制打开抖音身 https://v.douyin.com/YT98hjf/,看看【酸味真火的作品】坚持总会有收获的# 意想不到的情侣健身 https://v.douyin.com/YT982Sf/"; + System.out.println( RegexUtils.get("(https://)(.*?)(/)(.*?)(/)",url,0)); // String a = m.find() ? m.group(1) : url; // System.out.println(a); // int b = StringUtils.countOccurrencesOf(a, "https://v.douyin"); diff --git a/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java b/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java index 1689cb8..1c59a73 100644 --- a/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java +++ b/src/main/java/com/zh/project0512/controller/wxApp/VideoUController.java @@ -12,6 +12,7 @@ import com.zh.project0512.service.IVideoService; import com.zh.project0512.utils.JwtUtil; import com.zh.project0512.utils.MybatisPlusUtil; import com.zh.project0512.utils.RegexUtil; +import com.zh.project0512.utils.RegexUtils; import com.zh.project0512.utils.result.HttpStatusEnum; import com.zh.project0512.utils.result.Result; import io.jsonwebtoken.Claims; @@ -97,7 +98,7 @@ public class VideoUController { return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "已提交过该平台视频"); } // 校验并提取链接 - String s = new RegexUtil().httpReg(param.getUrl()); + String s = RegexUtils.get("(https://)(.*?)(/)(.*?)(/)",param.getUrl(),0); if(null == s){ return Result.fail(HttpStatusEnum.CUSTOM_EXCEPTION, "视频链接有误"); }else if(param.getType() == 1&&0==StringUtils.countOccurrencesOf(s,"https://v.douyin")){ diff --git a/src/main/java/com/zh/project0512/utils/RegexUtil.java b/src/main/java/com/zh/project0512/utils/RegexUtil.java index 7ab5fe3..68c0804 100644 --- a/src/main/java/com/zh/project0512/utils/RegexUtil.java +++ b/src/main/java/com/zh/project0512/utils/RegexUtil.java @@ -7,7 +7,7 @@ public class RegexUtil { // 截取字符串中https部分 public String httpReg(String url) { - Pattern p = Pattern.compile("((https://)+\\S*\\s)", Pattern.CASE_INSENSITIVE); + Pattern p = Pattern.compile("(https://)(.*?)(/)(.*?)(/)", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(url); String a = m.find() ? m.group(1) : null; return a; diff --git a/src/main/java/com/zh/project0512/utils/RegexUtils.java b/src/main/java/com/zh/project0512/utils/RegexUtils.java new file mode 100644 index 0000000..a323aea --- /dev/null +++ b/src/main/java/com/zh/project0512/utils/RegexUtils.java @@ -0,0 +1,153 @@ +package com.zh.project0512.utils; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * java 正则匹配基本操作工具类 + * 1. 多行匹配 + * 2. 已支持 . (默认正则 不支持) 匹配所有字符包括换行 + *

+ * 参考:https://www.cnblogs.com/Krloypower/p/11356805.html + * + * @description: + * @author: mabh + * @create: 2021/5/6 14:09 + **/ +public final class RegexUtils { + /** + * 缓存一些 Pattern.compile() 的 + */ + private static final Map PATTERN = new HashMap<>(); + + private RegexUtils() { + } + + public static String get(String regex, String content) { + return get(regex, content, 0); + } + + public static String get(String regex, String content, int groupIndex) { + return get(getPattern(regex), content, groupIndex); + } + + public static String get(Pattern pattern, String content) { + return get(pattern, content, 0); + } + + public static String get(Pattern pattern, String content, int groupIndex) { + Matcher matcher = pattern.matcher(content); + if (matcher.find()) { + return matcher.group(groupIndex); + } + return null; + } + + /** + * 获取匹配项 + * + * @param regex + * @param input + * @return 返回一个list集合类型的匹配项 + * @see #getMatcherList(String, CharSequence, int) + */ + public static List getMatcherList(String regex, CharSequence input) { + return getMatcherList(regex, input, 0); + } + + /** + * 获取匹配项 + * + * @param regex + * @param input + * @param group //group(0) 返回符合条件的字符串, group(1) 表示正则表达式中符合条件的字符串中的第一个() 中的字符串。 + * @return 返回一个list集合类型的匹配项 + * @see #getMatcherList(String, CharSequence) + */ + public static List getMatcherList(String regex, CharSequence input, int group) { + List list = new ArrayList<>(); + forEach(regex, input, matcher -> list.add(matcher.group(group))); + return list; + } + + /** + * 迭代匹配,更自由的操作,一般无需直接操作改方法,除非你有更复杂的场景。 + * + * @param regex + * @param input + * @param consumer + * @see #getMatcherList(String, CharSequence, int) + * @see #findAny(String, CharSequence) + */ + public static void forEach(String regex, CharSequence input, Consumer consumer) { + Matcher matcher = getMatcher(getPattern(regex), input); + while (matcher.find()) { + consumer.accept(matcher); + } + } + + /** + * 非贪婪模式匹配,是否有匹配项 + * + * @param regex + * @param input + * @return true,存在指定规则的匹配数据 + */ + public static boolean findAny(String regex, CharSequence input) { + //默认贪婪是匹配,如果仅仅查询是否有匹配项,直接是否非贪婪即可,性能更好,哪怕这种性能微乎其微 + return findAny(getPattern("^" + regex), input); + } + + public static boolean findAny(Pattern pattern, CharSequence input) { + return getMatcher(pattern, input).find(); + } + + /** + * 是否匹配 + * + * @param regex + * @param input + * @return + */ + public static boolean matches(String regex, CharSequence input) { + return getMatcher(getPattern(regex), input).matches(); + } + + /** + * 优先会找缓存,找不到再初始化 + * + * @param regex 标准的正则表达式 + * @return + */ + private static Pattern getPattern(String regex) { + Pattern pattern = PATTERN.getOrDefault(regex, Pattern.compile(regex,Pattern.DOTALL | Pattern.MULTILINE)); + PATTERN.put(regex, pattern); + return pattern; + } + + /** + * 获取匹配器,会缓存匹配器 + * + * @param pattern + * @param input + * @return + */ + private static Matcher getMatcher(Pattern pattern, CharSequence input) { + if (pattern == null) { + return null; + } + String regex = pattern.pattern(); + if (!PATTERN.containsKey(regex)) { + PATTERN.put(regex, pattern); + } + return pattern.matcher(input); + } + +} +