From 2d682afd2c0b270b94d7e7512e13e2c0ac4f2c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Thu, 7 May 2026 11:07:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/Extensions/StringExtensions.cs | 57 +++++++++++++++++++++++++++ Shared/General/Logger.cs | 2 +- Shared/Main.cs | 9 +++++ Shared/Utils/PathUtils.cs | 4 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Shared/Main.cs diff --git a/Shared/Extensions/StringExtensions.cs b/Shared/Extensions/StringExtensions.cs index 08aabbe..dd16a86 100644 --- a/Shared/Extensions/StringExtensions.cs +++ b/Shared/Extensions/StringExtensions.cs @@ -176,6 +176,63 @@ public static class StringExtensions { #endregion + #region 正则 + + /// + /// 搜索字符串中所有的正则匹配项。 + /// + public static IEnumerable RegexSearch(this string str, string pattern, RegexOptions options = RegexOptions.None) { + try { + return Regex.Matches(str, pattern, options).Cast().Select(m => m.Value); + } catch (Exception ex) { + Logger.Warning(ex, $"{nameof(RegexSearch)} 失败({pattern})"); + return []; + } + } + + /// + /// 获取字符串中的第一个正则匹配项,若无匹配或失败则返回 。 + /// + public static string? RegexSeek(this string str, string pattern, RegexOptions options = RegexOptions.None) { + try { + var match = Regex.Match(str, pattern, options); + return match.Success ? match.Value : null; + } catch (Exception ex) { + Logger.Warning(ex, $"{nameof(RegexSeek)} 失败({pattern})"); + return null; + } + } + + /// + /// 检查字符串是否匹配某正则模式。 + /// + public static bool RegexCheck(this string str, string pattern, RegexOptions options = RegexOptions.None) { + try { + return Regex.IsMatch(str, pattern, options); + } catch (Exception ex) { + Logger.Warning(ex, $"{nameof(RegexCheck)} 失败({pattern})"); + return false; + } + } + + /// + /// 进行正则替换。 + /// 失败时会抛出异常。 + /// + public static string RegexReplace(this string allContents, string searchRegex, string replaceTo, RegexOptions options = RegexOptions.None) { + return Regex.Replace(allContents, searchRegex, replaceTo, options); + } + + /// + /// 对每个正则匹配分别进行替换。 + /// 失败时会抛出异常。 + /// + public static string RegexReplace(this string allContents, string searchRegex, MatchEvaluator replaceTo, RegexOptions options = RegexOptions.None) { + return Regex.Replace(allContents, searchRegex, replaceTo, options); + } + + #endregion + /// /// 将第一个字符转换为大写,其余字符转换为小写。 /// diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs index f229c9b..d9ef7b3 100644 --- a/Shared/General/Logger.cs +++ b/Shared/General/Logger.cs @@ -56,7 +56,7 @@ public enum LogBehavior { } public static class Logger { - public static BaseLogger Instance { get; set; } = new(); + internal static BaseLogger Instance { get; set; } = new(); // 转发给实例的方法包装 public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") diff --git a/Shared/Main.cs b/Shared/Main.cs new file mode 100644 index 0000000..7bd84a5 --- /dev/null +++ b/Shared/Main.cs @@ -0,0 +1,9 @@ +namespace MeloongCore; +public static class Main { + + public static void Init(BaseLogger? logger = null) { + AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2)); + if (logger != null) Logger.Instance = logger; + } + +} diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index 53917c5..7264be4 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -88,8 +88,8 @@ public static class PathUtils { /// 去除路径末尾的文件名。 /// 若路径以分隔符结尾,则不作处理。 /// - public static string? RemoveFileName(string? path) { - if (path is null || string.IsNullOrEmpty(path)) return path; + public static string RemoveFileName(string? path) { + if (path is null || string.IsNullOrEmpty(path)) return ""; if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path; return Path.GetDirectoryName(path) ?? ""; }