// 示例:第一匹配和最长匹配
func main() {
b := []byte("abc1def1")
pat := `abc1|abc1def1`
reg1 := regexp.MustCompile(pat) // 第一匹配
reg2 := regexp.MustCompilePOSIX(pat) // 最长匹配
fmt.Printf("%s
", reg1.Find(b)) // abc1
fmt.Printf("%s
", reg2.Find(b)) // abc1def1
b = []byte("abc1def1")
pat = `(abc|abc1def)*1`
reg1 = regexp.MustCompile(pat) // 第一匹配
reg2 = regexp.MustCompilePOSIX(pat) // 最长匹配
fmt.Printf("%s
", reg1.Find(b)) // abc1
fmt.Printf("%s
", reg2.Find(b)) // abc1def1
}
实例
// 根据名字模糊搜索战队
func FuzzySearchWarTeam(name string) []*WarTeam {
ret := make([]*WarTeam, 0)
reg := regexp.MustCompile(regexp.QuoteMeta(name))
data, err := GetClubGlobalRedis().HGetAll(RedisKeyWarTeamName()).Result()
if err != nil {
antnet.LogInfo("search warteam by name failed err:%v", err)
return ret
}
for tname, tid := range data {
if reg.FindString(tname) != "" {
team := ModelWarTeam(antnet.Atoi32(tid))
if team != nil {
ret = append(ret, team)
}
}
if len(ret) > common.WarTeamMaxSearchNum {
break
}
}
return ret
}
https://www.cnblogs.com/golove/p/3270918.html