Remove pattern from string with gsub

前端 未结 4 1830
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 05:44

I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary:

a <- c(\         


        
相关标签:
4条回答
  • 2020-11-29 06:20

    Just to point out that there is an approach using functions from the tidyverse, which I find more readable than gsub:

    a %>% stringr::str_remove(pattern = ".*_")
    
    0 讨论(0)
  • 2020-11-29 06:31

    The following code works on your example :

    gsub(".*_", "", a)
    
    0 讨论(0)
  • 2020-11-29 06:33

    Alternatively, you can also try:

    gsub("\\S+_", "", a)
    
    0 讨论(0)
  • 2020-11-29 06:36
    as.numeric(gsub(pattern=".*_", replacement = '', a)
    [1] 5 7
    
    0 讨论(0)
提交回复
热议问题