英文:
How can I separate words in a string without deleting letters using str_replace?
问题
我想将字符串中的单词分开并在它们之间添加一个空格。如何在不删除某些字母的情况下使用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("_") |>
# 我想将字符串中的单词分开并在它们之间添加一个空格
stringr::str_replace_all("([a-z][A-Z])", " ")
#> [1] "Employe ame" "Em D" "Marrie D" "Marita tatu D"
#> [5] "Gende D" "Em tatu D"
我尝试了 stringr::str_replace_all("([a-z][A-Z])", " ")
,但这会删除与模式匹配的字母。
英文:
I want to separate the words in the string and add a space in between. How can I do it without deleting some letters by using 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"
<sup>Created on 2023-05-29 with reprex v2.0.2</sup>
I tried stringr::str_replace_all("([a-z][A-Z])", " "), but this removes the letters matched by the pattern.
答案1
得分: 2
使用gsub
函数与|
操作符,这只需一行代码。
gsub('_|(?<=[a-z])(?=[A-Z])', ' ', s1, perl=TRUE)
# [1] "员工姓名" "员工ID" "已婚ID" "婚姻状态ID"
# [5] "性别ID" "员工状态ID"
英文:
Using OR operator |
with gsub
it's just a one-liner.
gsub('_|(?<=[a-z])(?=[A-Z])', ' ', s1, perl=TRUE)
# [1] "Employee Name" "Emp ID" "Married ID" "Marital Status ID"
# [5] "Gender ID" "Emp Status ID"
答案2
得分: 1
你想要使用回顾前瞻和回顾后顾:
library(stringr)
s1 |>
str_remove_all("_") |>
str_replace_all("(?<=[a-z])(?=[A-Z])", " ")
# [1] "员工姓名" "员工ID" "已婚ID"
# [4] "婚姻状态ID" "性别ID" "员工状态ID"
或者,您也可以使用捕获组和反向引用的方法:
s1 |>
str_remove_all("_") |>
str_replace_all("([a-z])([A-Z])", "\ \")
# [1] "员工姓名" "员工ID" "已婚ID"
# [4] "婚姻状态ID" "性别ID" "员工状态ID"
英文:
You want to use a lookbehind and lookahead:
library(stringr)
s1 |>
str_remove_all("_") |>
str_replace_all("(?<=[a-z])(?=[A-Z])", " ")
# [1] "Employee Name" "Emp ID" "Married ID"
# [4] "Marital Status ID" "Gender ID" "Emp Status ID"
Or alternatively, capture groups with backreferences:
s1 |>
str_remove_all("_") |>
str_replace_all("([a-z])([A-Z])", "\ \")
# [1] "Employee Name" "Emp ID" "Married ID"
# [4] "Marital Status ID" "Gender ID" "Emp Status ID"
答案3
得分: 0
s1 <- c("Employee Name", "Emp ID", "Married ID", "Marital Status ID", "Gender ID", "Emp Status ID")
str_replace_all(s1, '([A-Z]+)', ' \') |
str_replace_all('_', ' ') |
str_squish() |
print()
英文:
s1 <- c("Employee_Name", "EmpID", "MarriedID", "MaritalStatusID", "GenderID",
"EmpStatusID")
str_replace_all(s1, '([A-Z]+)', ' \') |>
str_replace_all('_', ' ') |>
str_squish() |>
print()
[1] "Employee Name" "Emp ID" "Married ID" "Marital Status ID" "Gender ID" "Emp Status ID"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论