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

关键正则表达式:

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
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章