- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我做错了什么?为什么 ToUpper 不起作用?
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
r := regexp.MustCompile("(\\w)(\\w+)")
// Getting "sometext" instead of "SomeText"
res := r.ReplaceAllString("some text", strings.ToUpper("$1") + "$2")
fmt.Println(res)
}
最佳答案
恐怕你不能那样使用 $1
和 $2
!
我认为您正试图将“一些文本”变成“一些文本”。
Here is an alternative solution
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
r := regexp.MustCompile(`\s*\w+\s*`)
res := r.ReplaceAllStringFunc("some text", func(s string) string {
return strings.Title(strings.TrimSpace(s))
})
fmt.Println(res)
}
关于go - 使用 ReplaceAllString 和 ToUpper 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16456184/
我正在用 Go 编程语言编写一个聊天机器人程序。在此函数中,它读取用户字符串以检查正则表达式,然后删除该表达式并在找到时替换为另一个字符串。它成功找到匹配项,但不会将其附加到字符串 input = "
使用以下代码: package main import ( "regexp" ) var namedMatch = regexp.MustCompile("\\{([^}]+)\\}") fu
我正在尝试编写一些正则表达式来匹配和分离看起来像软件版本的字符串(但实际上并非如此,例如,semver 解析将不起作用)。我在匹配输入字符串的“前缀”和“后缀”部分时遇到了一些问题。 我从下面得到的输
尝试将单词开头的每个字母大写。我知道有 strings.Title,但这对我的需要来说太不精确了。 我不确定为什么这不起作用: package main import ( "fmt"
这play清楚地证明了我的困境。 最终我试图将一个不守规矩的字符串拆分成单词。对我来说,“2015”是一个词,“100 美元”也是一个词,但如果输入是“One.2wo, (three)”,我想要 [O
我正在尝试使用我找到的正则表达式将 CamelCase 转换为 snake_case here .这是我正在使用的代码片段: in := "camelCase" var re1 = regexp.Mu
我做错了什么?为什么 ToUpper 不起作用? package main import ( "fmt" "regexp" "strings" ) func main() {
我是一名优秀的程序员,十分优秀!