在 golang 中使用正则表达式,我想用 -
替换任何语言的所有非字母数字字符,以制作漂亮的 url:
Here是我尝试过的众多正则表达式之一:
主要包
import (
"fmt"
"regexp"
)
const sample = `سلام دنیا hello world 1 %^&`
func main() {
var re = regexp.MustCompile(`~[\p{L}0-9\s]+`)
s := re.ReplaceAllString(sample, `-`)
fmt.Println(s)
}
输出应该是:
سلام-دنیا-hello-world-1
但它不起作用。我该如何解决?
最佳答案
关于regex - 如何替换任何语言的所有非字母数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57159355/