I have a regex as following (ORDER\s+BY)|(LIMIT)|$
. I want to insert a substring before the first match of the regex. I am looking for a pure regexp solution in Golang not finding the index and then adding a substring. Since Golang only has regexp.ReplaceAll func which replaces all the matches, not the first one.
exp := regexp.MustCompile(`(ORDER\s+BY)|(LIMIT)|$`)
fmt.Println(exp.ReplaceAllString(str, "..."))
Example
Input: abcd ORDER BY LIMIT
substring=GROUP BY
Expected output: abcd GROUP BY ORDER BY LIMIT
Input: abcd LIMIT
Expected output: abcd GROUP BY LIMIT