How to split a string with multiple delimiters in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "regexp"
)

func main() {
    s := "aaa : bbb / ccc . ddd ? eee&fff - ggg_hhh | iii \n jjj"

    // Define the delimiters (comma, space, exclamation mark, question mark)
    delimiters := regexp.MustCompile(`[,\s!?\-_|/\n.:&]+`) // Corrected regex

    // Split the string using the defined delimiters
    words := delimiters.Split(s, -1)

    for _, value := range words {
            fmt.Printf("%s\n", value)
    }
}



/*
run:
     
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj

*/
 

 



answered Mar 4, 2025 by avibootz
...