dpsx99068 2016-07-20 05:18
浏览 748
已采纳

Golang regexp.ReplaceAllString忽略替换字符串“ $ X_”

I'm trying to convert CamelCase to snake_case using the regex I found here. Here's a snippet of the code I'm using:

in := "camelCase"
var re1 = regexp.MustCompile(`(.)([A-Z][a-z]+)`)
out := re1.ReplaceAllString(in, "$1_$2")

The regex will match lCase. $1 here is l and $2 is Case, so using the replacement string "$1_$2" should result in camel_Case. Instead, it results in cameCase.

Changing the replacement string to "$1_" results in came. If I change it to "$1+$2", the result will be camel+Case as expected (see playground).

Right now, my workaround is to use "$1+$2" as the replacement string, and then using strings.Replace to change the plus sign to an underscore. Is this a bug or am I doing something wrong here?

  • 写回答

1条回答 默认 最新

  • dongsu3664 2016-07-20 05:36
    关注

    The fix is to use ${1}_$2 (or ${1}_${2} for symmetry).

    Per https://golang.org/pkg/regexp/#Regexp.Expand (my emphasis):

    In the template, a variable is denoted by a substring of the form $name or ${name}, where name is a non-empty sequence of letters, digits, and underscores.

    ...

    In the $name form, name is taken to be as long as possible: $1x is equivalent to ${1x}, not ${1}x, and, $10 is equivalent to ${10}, not ${1}0.

    So in $1_$2, you're actually looking for a group named 1_ and then another group named 2 and putting them together.

    As to why using $1_$2 (or $foo$2 for that matter) results in "cameCase," that same documentation says:

    A reference to an out of range or unmatched index or a name that is not present in the regular expression is replaced with an empty slice.

    So replacing with "$1_$2" is equivalent to replacing with just "$2".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退