关键正则表达式:
ok, _ := regexp.MatchString("^[A-Za-z\\d\u4e00-\u9fa5]+(,[A-Za-z\\d\u4e00-\u9fa5]+)*$", text)
源代码:
func MatchIds(text string) ([]int64, []string) {
int64Ids := make([]int64, 0)
stringIds := make([]string, 0)
// 把可能的分隔符,统一替换成英文 逗号 ,
text = strings.ReplaceAll(text, ",", ",")
text = strings.ReplaceAll(text, "/", ",")
text = strings.ReplaceAll(text, "\n", ",")
text = strings.ReplaceAll(text, " ", ",")
fmt.Println("text=", text)
// 支持数字,字母,中文
ok, _ := regexp.MatchString("^[A-Za-z\\d\u4e00-\u9fa5]+(,[A-Za-z\\d\u4e00-\u9fa5]+)*$", text)
if ok {
ids := strings.Split(text, ",")
int64Ids = convert.ToInt64Slice(ids)
if len(int64Ids) == 0 {
stringIds = convert.ToStringSlice(ids)
}
}
return int64Ids, stringIds
}
网友评论