picgo/corelib/regexp.go

39 lines
916 B
Go
Raw Permalink Normal View History

2024-07-12 20:32:33 +08:00
package corelib
import (
"github.com/dlclark/regexp2"
)
// 执行正则
func regexpRun(rx string, txt string) (ok bool) {
var (
err error
compile *regexp2.Regexp
matchString bool
)
if compile, err = regexp2.Compile(rx, 0); err != nil {
Logger.Error("regexp compile error:", err)
return false
}
if matchString, err = compile.MatchString(txt); err != nil {
Logger.Error("regexp compile error:", err)
return false
}
return matchString
}
// IsCaptcha 验证码正则
func IsCaptcha(code string) (ok bool) {
return regexpRun("^[0-9a-zA-Z]{1,5}$", code)
}
// CheckUsername 用户名正则
func CheckUsername(username string) (ok bool) {
return regexpRun("^[a-zA-Z0-9_-]{6,16}$", username)
}
// CheckPassword 用户名正则
func CheckPassword(password string) (ok bool) {
return regexpRun("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!#%*?&])[A-Za-z\\d$@#$!%*?&]{8,16}", password)
}