Remove all characters before a period in a string

前端 未结 5 1674
独厮守ぢ
独厮守ぢ 2020-12-09 18:01

This keeps everything before a period:

gsub(\"\\\\..*\",\"\", data$column )

how to keep everything after the period?

5条回答
  •  执笔经年
    2020-12-09 18:34

    You could use stringi with lookbehind regex

     library(stringi)
     stri_extract_first_regex(data1, "(?<=\\.).*")
     #[1] "bar.barfoo"
     stri_extract_first_regex(data, "(?<=\\.).*")
     #[1] "barfoo"
    

    If the string doesn't have ., this retuns NA (it is not clear about how to deal with this in the question)

     stri_extract_first_regex(data2, "(?<=\\.).*")
     #[1] NA
    
    ###data
    data <- 'foobar.barfoo' 
    data1 <- 'foo.bar.barfoo'
    data2 <- "foobar"
    

提交回复
热议问题