Find a word before one of two possible separators

后端 未结 4 1062
故里飘歌
故里飘歌 2021-01-19 02:45
word:12335
anotherword:2323434
totallydifferentword/455
word/32

I need to grab the character string before the : or / usi

4条回答
  •  孤城傲影
    2021-01-19 03:05

    You could use the package unglue :

    library(unglue)
    x <- c("word:12335", "anotherword:2323434", "totallydifferentword/455", "word/32")
    unglue_vec(x, "{res}{=[:/].*?}")
    #> [1] "word"                 "anotherword"          "totallydifferentword"
    #> [4] "word"
    

    Created on 2019-10-08 by the reprex package (v0.3.0)

    • {res} matches anything and will be returned, it's equivalent to {res=.*?}
    • {=[:/].*?} matches anything starting with : or / and won't be returned as we have no lhs to =

提交回复
热议问题