下面的示例代码实现了在 go 语言中,使用正则表达式过滤字符,只保留数字和大小写字母。

package main

import (
"fmt"
"log"
"regexp"
)

func main() {

example := "#GoLangCode!$!"

// Make a Regex to say we only want letters and numbers
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
log.Fatal(err)
}
processedString := reg.ReplaceAllString(example, "")

fmt.Printf("A string of %s becomes %s \n", example, processedString)
}
(adsbygoogle = window.adsbygoogle || []).push({});