2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-05 04:37:17 +08:00
Files
panel/internal/http/rule/regex.go

30 lines
513 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package rule
import (
"regexp"
"strings"
"github.com/go-playground/validator/v10"
)
type Regex struct{}
func NewRegex() *Regex {
return &Regex{}
}
func (r *Regex) Regex(fl validator.FieldLevel) bool {
// 从标签中获取正则,格式类似于 `regex=^[a-zA-Z0-9_]+$`
pattern := fl.Param()
// 替换转义字符
pattern = strings.ReplaceAll(pattern, "", ",")
re, err := regexp.Compile(pattern)
if err != nil {
return false
}
value := fl.Field().String()
return re.MatchString(value)
}