如何在不删除字母的情况下使用str_replace分隔字符串?

huangapple go评论54阅读模式
英文:

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(&#39;_|(?&lt;=[a-z])(?=[A-Z])&#39;, &#39; &#39;, s1, perl=TRUE)
# [1] &quot;Employee Name&quot;     &quot;Emp ID&quot;            &quot;Married ID&quot;        &quot;Marital Status ID&quot;
# [5] &quot;Gender ID&quot;         &quot;Emp Status ID&quot;

答案2

得分: 1

你想要使用回顾前瞻和回顾后顾:

library(stringr)

s1 |&gt;
  str_remove_all(&quot;_&quot;) |&gt;
  str_replace_all(&quot;(?&lt;=[a-z])(?=[A-Z])&quot;, &quot; &quot;)
# [1] &quot;员工姓名&quot;         &quot;员工ID&quot;            &quot;已婚ID&quot;       
# [4] &quot;婚姻状态ID&quot;     &quot;性别ID&quot;         &quot;员工状态ID&quot;

或者,您也可以使用捕获组和反向引用的方法:

s1 |&gt;
  str_remove_all(&quot;_&quot;) |&gt;
  str_replace_all(&quot;([a-z])([A-Z])&quot;, &quot;\ \&quot;)
# [1] &quot;员工姓名&quot;         &quot;员工ID&quot;            &quot;已婚ID&quot;       
# [4] &quot;婚姻状态ID&quot;     &quot;性别ID&quot;         &quot;员工状态ID&quot;
英文:

You want to use a lookbehind and lookahead:

library(stringr)

s1 |&gt;
  str_remove_all(&quot;_&quot;) |&gt;
  str_replace_all(&quot;(?&lt;=[a-z])(?=[A-Z])&quot;, &quot; &quot;)
# [1] &quot;Employee Name&quot;     &quot;Emp ID&quot;            &quot;Married ID&quot;       
# [4] &quot;Marital Status ID&quot; &quot;Gender ID&quot;         &quot;Emp Status ID&quot;

Or alternatively, capture groups with backreferences:

s1 |&gt;
  str_remove_all(&quot;_&quot;) |&gt;
  str_replace_all(&quot;([a-z])([A-Z])&quot;, &quot;\ \&quot;)
# [1] &quot;Employee Name&quot;     &quot;Emp ID&quot;            &quot;Married ID&quot;       
# [4] &quot;Marital Status ID&quot; &quot;Gender ID&quot;         &quot;Emp Status ID&quot;

答案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 &lt;- c(&quot;Employee_Name&quot;, &quot;EmpID&quot;, &quot;MarriedID&quot;, &quot;MaritalStatusID&quot;, &quot;GenderID&quot;, 
        &quot;EmpStatusID&quot;)
str_replace_all(s1, &#39;([A-Z]+)&#39;, &#39; \&#39;) |&gt;
  str_replace_all(&#39;_&#39;, &#39; &#39;) |&gt;
  str_squish() |&gt;
  print()
[1] &quot;Employee Name&quot;     &quot;Emp ID&quot;            &quot;Married ID&quot;        &quot;Marital Status ID&quot; &quot;Gender ID&quot;         &quot;Emp Status ID&quot;    

huangapple
  • 本文由 发表于 2023年5月30日 06:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定