我想把字符串中的单词分开,并在它们之间加一个空格。我怎么才能做到这一点,而不删除一些字母使用str_replace?
s1 <- c("Employee_Name", "EmpID", "MarriedID", "MaritalStatusID", "GenderID",
"EmpStatusID") |> print()
#> [1] "Employee_Name" "EmpID" "MarriedID" "MaritalStatusID"
#> [5] "GenderID" "EmpStatusID"
s1|>
stringr::str_remove_all("_") |>
# I want to separate the words in the string and add a space in between
stringr::str_replace_all("([a-z][A-Z])", " ")
#> [1] "Employe ame" "Em D" "Marrie D" "Marita tatu D"
#> [5] "Gende D" "Em tatu D"
创建于2023-05-29带有reprex v2.0.2
我尝试了stringr::str_replace_all(“([a-z][A-Z])",““),但这会删除与模式匹配的字母。
3条答案
按热度按时间cwtwac6a1#
使用OR操作符
|
和gsub
,这只是一行代码。np8igboo2#
您希望使用lookbehind和lookahead:
或者,使用反向引用捕获组:
ztmd8pv53#