Hello
Can sombody suggest way to realize multipattern replace?
93881231212 -> 13881231212
0013881231212 -> 13881231212
+13881231212  -> 13881231212

somthing like this 


https://play.golang.org/p/Abeq2-FV7NW



import (
        "fmt"
                "regexp"
        "strings"
)       

func main() {
        fmt.Println("Hello, 世界")
        r := regexp.MustCompile(`^9(\d{10})$|^00(.*)$|^\+(.*)`)
        fmt.Println(ReplaceAllStringGroup("93881231212", `1$1|$2|$3`, r))
        fmt.Println(ReplaceAllStringGroup("0013881231212", `0$1|$2|$3`, r))
        fmt.Println(ReplaceAllStringGroup("+13881231212", `0$1|$2|$3`, r))
        fmt.Println(ReplaceAllStringGroup("-13881231212", `0$1|$2|$3`, r))
}

func ReplaceAllStringGroup(s string, repl string, r *regexp.Regexp) string {
        splitted := strings.Split(repl, "|")
        list := r.FindStringSubmatch(s)
        if len(list) != len(splitted)+1 {
                return "not matched"
        }
        for i, m := range list[1:] {
                if m != "" {
                        return r.ReplaceAllString(s, splitted[i])
                }
        }
        return "not matched"
}

-- 
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/777f0d0a-6099-407e-b8b8-d02b01ea6ddao%40googlegroups.com.

Reply via email to