package tools import ( "errors" "regexp" ) func GetOneStringByRegex(str, rule string) (string, error) { reg, err := regexp.Compile(rule) if reg == nil || err != nil { return "", errors.New("正则MustCompile错误:" + err.Error()) } //提取关键信息 result := reg.FindStringSubmatch(str) if len(result) < 1 { return "", errors.New("没有获取到子字符串") } return result[1], nil } func ReplaceStringByRegex(str, rule, replace string) (string, error) { reg, err := regexp.Compile(rule) if reg == nil || err != nil { return "", errors.New("正则MustCompile错误:" + err.Error()) } return reg.ReplaceAllString(str, replace), nil }