提取第二个下划线和点号之前的字符串: R

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

Extracting string after second underscore and before dot R

问题

在第二个下划线和句点之间保留字符串。谢谢!

x <- "Exche_log_group_2.csv", "Exche_log_group_3.csv"

期望输出:

group_2  group_3
英文:

I'm trying to keep strings after the second underscore and before dot. Thanks!

x <- "Exche_log_group_2.csv", "Exche_log_group_3.csv"

Desired output:

group_2  group_3

答案1

得分: 2

使用 gsub

gsub("[^_]+_[^_]+_|\\..*", "", x)
[1] "group_2" "group_3"
英文:

use gsub

gsub("[^_]+_[^_]+_|\\..*", "", x)
[1] "group_2" "group_3"

答案2

得分: 2

使用带有捕获组的 sub 我们可以尝试:

x <- c("Exche_log_group_2.csv", "Exche_log_group_3.csv")
output <- sub(".*?([^_]+_\\d+)\\.[^.]+$", "\", x)
output  # [1] "group_2" "group_3"
英文:

Using sub with a capture group we can try:

<!-- language: r -->

x &lt;- c(&quot;Exche_log_group_2.csv&quot;, &quot;Exche_log_group_3.csv&quot;)
output &lt;- sub(&quot;.*?([^_]+_\\d+)\\.[^.]+$&quot;, &quot;\&quot;, x)
output  # [1] &quot;group_2&quot; &quot;group_3&quot;

huangapple
  • 本文由 发表于 2023年6月5日 09:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403025.html
匿名

发表评论

匿名网友

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

确定