Go 语言正则匹配 ID 逗号分隔 数字、英文字母、中文

2022-11-06 09:33:22 浏览数 (1)

关键正则表达式:

代码语言:javascript复制
ok, _ := regexp.MatchString("^[A-Za-z\du4e00-u9fa5] (,[A-Za-z\du4e00-u9fa5] )*$", text)

源代码:

代码语言:javascript复制
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\du4e00-u9fa5] (,[A-Za-z\du4e00-u9fa5] )*$", text)

    if ok {
        ids := strings.Split(text, ",")
        int64Ids = convert.ToInt64Slice(ids)

        if len(int64Ids) == 0 {
            stringIds = convert.ToStringSlice(ids)
        }
    }
    return int64Ids, stringIds
}

0 人点赞