修复正则匹配字符串不生效的问题

This commit is contained in:
Alex Yang
2025-11-29 00:23:20 +08:00
parent 3207510c91
commit 0468f52050
2 changed files with 7 additions and 8 deletions

View File

@@ -374,12 +374,11 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
}
case strings.HasPrefix(line, "/") && strings.HasSuffix(line, "/"):
// 关键字匹配规则:/keyword/ 格式,不区分大小写,字面量匹配特殊字符
keyword := strings.TrimPrefix(strings.TrimSuffix(line, "/"), "/")
// 转义特殊字符,确保字面量匹配
quotedKeyword := regexp.QuoteMeta(keyword)
// 编译为不区分大小写的正则表达式,匹配域名中任意位置
if re, err := regexp.Compile("(?i)" + quotedKeyword); err == nil {
// 正则表达式匹配规则:/regex/ 格式,不区分大小写
pattern := strings.TrimPrefix(strings.TrimSuffix(line, "/"), "/")
// 编译为不区分大小写的正则表达式,确保能匹配域名中任意位置
// 对于像 /domain/ 这样的规则,应该匹配包含 domain 字符串的任何域名
if re, err := regexp.Compile("(?i).*" + regexp.QuoteMeta(pattern) + ".*"); err == nil {
// 保存原始规则字符串
m.addRegexRule(re, line, !isException, isLocal, source)
}