慕标5832272
这似乎(.*)是一场贪婪的比赛,因此您应该尝试限制第一组。根据您的样本输入,这|about是可选的。var re = regexp.MustCompile(`\[\[([^|]*)(?:\|(.*))?\]\]`)func relink(input string) string { var reform []string for _, match := range re.FindAllStringSubmatch(input, -1) { name, short := match[1], match[2] if short == "" { short = strings.ToLower(name) } reform = append(reform, fmt.Sprintf("[%s](%s)", name, short)) } return strings.Join(reform, "\n")}操场