douzhouhan4618
2015-07-28 14:53
浏览 213
已采纳

带有反向引用的golang regexp ReplaceAllStrings不太起作用

Trying to capitalize each letter at begin of the word. I know that there is strings.Title, but that is too imprecise for my needs.

I am not sure why this does not work:

package main

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

func main() {
    re := regexp.MustCompile(`\b([a-z])`)
    fmt.Println(re.ReplaceAllString("my test string", strings.ToUpper("$1")))

}

https://play.golang.org/p/C-8QG1FrOi

图片转代码服务由CSDN问答提供 功能建议

尝试在单词开头将每个字母大写。 我知道有 strings.Title </ code>,但这对于我的需求来说太不精确了。 </ p>

我不确定为什么这行不通:</ p>

  package main 
 
import(
“ fmt” 
“  regexp“ 
” strings“ 
)
 
func main(){
 re:= regexp.MustCompile(`\ b([az])`)
 fmt.Println(re.ReplaceAllString(” my test 字符串”,strings.ToUpper(“ $ 1”)))
 
} 
 </ code> </ pre> 
 
 

https://play.golang.org/p/C-8QG1FrOi </ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题