go字符串下划线匹配
作者:互联网
将字符串以下划线分割
下面的代码会将给定的字符串以下划线分割。
import (
"fmt"
"strings"
"regexp"
)
func main() {
snakeCase := ConvertToSnakeCase("ILikeProgrammingINGo123")
fmt.Println("String in snake case : ", snakeCase)
}
func ConvertToSnakeCase(input string) string {
var matchChars = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAlpha = regexp.MustCompile("([a-z0-9])([A-Z])")
snake := matchChars.ReplaceAllString(input, "${1}_${2}")
snake = matchAlpha.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
output:
String in snake case : i_like_programming_in_go123
标签:下划线,fmt,matchChars,字符串,snake,go,regexp 来源: https://www.cnblogs.com/cheyunhua/p/16241085.html