golang如何使用正则表达式
package main
import (
"fmt"
"regexp"
//"strings"
)
type User struct{
Id int
}
func main() {
r, _ := regexp.Compile("^[\u4E00-\u9FA5]*$")
fmt.Println(r.MatchString("我"))
//true
}
正则表达式以^开头,$结束!
推荐一款非常好用轻便的正则框架
main.go
package main
import (
"github.com/fwhezfwhez/superChecker"
"fmt"
"log"
)
type User struct {
UserName string `superChecker:"userName" json:"userName"`
Password string `superChecker:"password"`
Phone string `superChecker:"mobilephone|telephone"`
Text string `superChecker:"length,chineseOnly,notNull"`
}
func main(){
user := User{
"",
"a1dfdasfsdf",
"12578854875",
"中",
}
checker :=superChecker.GetChecker()
checker.AddRegex("passWoRd","^[\\s\\S]{6,}$")
checker.AddRegex("length","^[\\s\\S]{0,20}$")
checker.AddRegex("chineseOnly","^[\u4E00-\u9FA5]*$")
result,msg,err :=checker.SuperCheck(user)
if err!=nil {
log.Println(err)
}
fmt.Println("匹配结果:",result,"信息:",msg)
checker.AddDefaultRegex("chineseOnly","^[\u4E00-\u9FA5]*$")
checker.ListDefault()
checker.ListRegexBuilder()
checker.ListAll()
}
以后处理前端的数据,就不用单独get,并各种ifCheckxxxxx
使用这种注解模式来应对前端请求,User是响应的VO模型,前端可能会发送username password,phone等数据
给vo加上superChecker注解,check一步达成!