On Thursday, 3 September 2020 10:15:29 UTC+1, Emilius Omeen wrote: > > pkg "regexp" not have function which allow many group replace, only have > ReplaceAllString > > > If I just take the examples you posted at the start, I can do that with a single ReplaceAllString: https://play.golang.org/p/9haO39qV0K2 https://play.golang.org/p/v3dLbTKHCtR
I think the original problem definition doesn't explain why it needs more than this - maybe you need some more test cases. You might find Expand <https://golang.org/pkg/regexp/#Regexp.Expand> / ExpandString or ReplaceAllStringFunc useful. Note that you can write your original regexp with capture groups to distinguish which branch matched: r := regexp.MustCompile(`(^9(\d{10})$)|(^00(.*)$)|(^\+(.*))`) ^ ^ ^ ^ ^ ^ $1 $2 $3 $4 $5 $6 Using ...Submatch or ...SubmatchIndex, you can identify which of the branches matched. If your regexp matches the first alternative, $1 (and $2) will be set. If it matches the second alternative, $3 (and $4) will be set. And so on. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2c01fc2a-6a16-47d5-8d22-c6dd501e9d9do%40googlegroups.com.