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

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

How can I separate words in a string without deleting letters using str_replace?

问题

我想将字符串中的单词分开并在它们之间添加一个空格。如何在不删除某些字母的情况下使用str_replace来实现?

  1. s1 <- c("Employee_Name", "EmpID", "MarriedID", "MaritalStatusID", "GenderID",
  2. "EmpStatusID") |> print()
  3. #> [1] "Employee_Name" "EmpID" "MarriedID" "MaritalStatusID"
  4. #> [5] "GenderID" "EmpStatusID"
  5. s1|>
  6. stringr::str_remove_all("_") |>
  7. # 我想将字符串中的单词分开并在它们之间添加一个空格
  8. stringr::str_replace_all("([a-z][A-Z])", " ")
  9. #> [1] "Employe ame" "Em D" "Marrie D" "Marita tatu D"
  10. #> [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?

  1. s1 <- c("Employee_Name", "EmpID", "MarriedID", "MaritalStatusID", "GenderID",
  2. "EmpStatusID") |> print()
  3. #> [1] "Employee_Name" "EmpID" "MarriedID" "MaritalStatusID"
  4. #> [5] "GenderID" "EmpStatusID"
  5. s1|>
  6. stringr::str_remove_all("_") |>
  7. # I want to separate the words in the string and add a space in between
  8. stringr::str_replace_all("([a-z][A-Z])", " ")
  9. #> [1] "Employe ame" "Em D" "Marrie D" "Marita tatu D"
  10. #> [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函数与|操作符,这只需一行代码。

  1. gsub('_|(?<=[a-z])(?=[A-Z])', ' ', s1, perl=TRUE)
  2. # [1] "员工姓名" "员工ID" "已婚ID" "婚姻状态ID"
  3. # [5] "性别ID" "员工状态ID"
英文:

Using OR operator | with gsub it's just a one-liner.

  1. gsub(&#39;_|(?&lt;=[a-z])(?=[A-Z])&#39;, &#39; &#39;, s1, perl=TRUE)
  2. # [1] &quot;Employee Name&quot; &quot;Emp ID&quot; &quot;Married ID&quot; &quot;Marital Status ID&quot;
  3. # [5] &quot;Gender ID&quot; &quot;Emp Status ID&quot;

答案2

得分: 1

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

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

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

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

You want to use a lookbehind and lookahead:

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

Or alternatively, capture groups with backreferences:

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

答案3

得分: 0

  1. s1 <- c("Employee Name", "Emp ID", "Married ID", "Marital Status ID", "Gender ID", "Emp Status ID")
  2. str_replace_all(s1, '([A-Z]+)', ' \') |
  3. str_replace_all('_', ' ') |
  4. str_squish() |
  5. print()
英文:
  1. s1 &lt;- c(&quot;Employee_Name&quot;, &quot;EmpID&quot;, &quot;MarriedID&quot;, &quot;MaritalStatusID&quot;, &quot;GenderID&quot;,
  2. &quot;EmpStatusID&quot;)
  3. str_replace_all(s1, &#39;([A-Z]+)&#39;, &#39; \&#39;) |&gt;
  4. str_replace_all(&#39;_&#39;, &#39; &#39;) |&gt;
  5. str_squish() |&gt;
  6. print()
  1. [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:

确定