From 9375900290b66e84bd85812d239ffb1d848daeff Mon Sep 17 00:00:00 2001 From: Wangyanshu <2680603193@qq.com> Date: Thu, 7 May 2026 15:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20'=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=88=AB=E5=90=8D+URL=E5=B7=A5=E5=85=B7=E7=B1=BB'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 命令别名+URL工具类 | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 命令别名+URL工具类 diff --git a/命令别名+URL工具类 b/命令别名+URL工具类 new file mode 100644 index 0000000..8fcb4fd --- /dev/null +++ b/命令别名+URL工具类 @@ -0,0 +1,40 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Pattern; + +public class CommandUtil { + // 命令别名映射 + private static Map aliasMap; + // URL正则校验 + private static final Pattern URL_REG = Pattern.compile("^https?://.+"); + + static { + aliasMap = new HashMap<>(); + // 作业要求:别名 c 代表 crawl + aliasMap.put("c", "crawl"); + // 可扩展其他别名 + aliasMap.put("h", "history"); + aliasMap.put("e", "exit"); + } + + // 解析别名,返回真实命令 + public static String parseAlias(String input) { + String[] arr = input.split(" ", 2); + String cmd = arr[0]; + // 如果有别名就替换 + if(aliasMap.containsKey(cmd)){ + cmd = aliasMap.get(cmd); + } + // 拼接参数 + if(arr.length > 1){ + return cmd + " " + arr[1]; + } + return cmd; + } + + // 校验URL是否合法 + public static boolean checkUrl(String url) { + if(url == null || url.isBlank()) return false; + return URL_REG.matcher(url).matches(); + } +} \ No newline at end of file