From f19f4c6f7254405516e5c2a150bc94d3e2657bda Mon Sep 17 00:00:00 2001 From: ywmoyue Date: Sat, 1 Jun 2024 11:04:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5/=E5=AF=BC=E5=85=A5=E5=BC=B9?= =?UTF-8?q?=E5=B9=95=E5=B1=8F=E8=94=BD=E8=AF=8D=E6=97=B6=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=9D=9E=E6=B3=95=E6=AD=A3=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BiliLite.UWP/BiliLite.UWP.csproj | 1 + .../Extensions/RegexExtensions.cs | 24 +++++++++++++++++++ src/BiliLite.UWP/Modules/SettingVM.cs | 5 ++++ 3 files changed, 30 insertions(+) create mode 100644 src/BiliLite.UWP/Extensions/RegexExtensions.cs diff --git a/src/BiliLite.UWP/BiliLite.UWP.csproj b/src/BiliLite.UWP/BiliLite.UWP.csproj index c46dd6e8..3b629933 100644 --- a/src/BiliLite.UWP/BiliLite.UWP.csproj +++ b/src/BiliLite.UWP/BiliLite.UWP.csproj @@ -157,6 +157,7 @@ + diff --git a/src/BiliLite.UWP/Extensions/RegexExtensions.cs b/src/BiliLite.UWP/Extensions/RegexExtensions.cs new file mode 100644 index 00000000..30a65cc7 --- /dev/null +++ b/src/BiliLite.UWP/Extensions/RegexExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.Text.RegularExpressions; + +namespace BiliLite.Extensions +{ + public static class RegexExtensions + { + public static bool IsValidRegex(this string regexString) + { + if (string.IsNullOrEmpty(regexString)) return false; + + try + { + var regex = new Regex(regexString); + return true; + } + catch (Exception) + { + // 语法不正确的正则表达式会导致ArgumentException异常 + return false; + } + } + } +} diff --git a/src/BiliLite.UWP/Modules/SettingVM.cs b/src/BiliLite.UWP/Modules/SettingVM.cs index 47a29950..40d92fc6 100644 --- a/src/BiliLite.UWP/Modules/SettingVM.cs +++ b/src/BiliLite.UWP/Modules/SettingVM.cs @@ -216,6 +216,11 @@ private void ImportDanmakuFilterCore(List filterList) ShieldRegulars.Clear(); foreach (var item in ls) { + if (!item.IsValidRegex()) + { + logger.Warn("非法正则表达式: " + item); + continue; + } ShieldRegulars.Add(item); } SettingService.SetValue(SettingConstants.VideoDanmaku.SHIELD_REGULAR, ShieldRegulars);