【发布时间】:2016-08-19 10:26:43
【问题描述】:
大多数stringr 函数只是对应stringi 函数的包装。 str_replace_all 就是其中之一。然而我的代码不适用于stri_replace_all,对应的stringi 函数。
我正在编写一个快速的正则表达式来将驼峰大小写(的一个子集)转换为间隔单词。
我很困惑为什么会这样:
str <- "thisIsCamelCase aintIt"
stringr::str_replace_all(str,
pattern="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this Is Camel Case ain't It"
这不是:
stri_replace_all(str,
regex="(?<=[a-z])([A-Z])",
replacement=" \\1")
# "this 1s 1amel 1ase ain't 1t"
【问题讨论】:
-
一个选项是
stri_replace_all(str, regex = "(?<=[a-z])(?=[A-Z])", replacement=" ")