Find a word before one of two possible separators

后端 未结 4 1013
故里飘歌
故里飘歌 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:02

    Maybe try:

    x <- c("word:12335", "anotherword:2323434", "totallydifferentword/455", "word/32")
    lapply(strsplit(x, ":|/"), function(z) z[[1]]) #as a list
    sapply(strsplit(x, ":|/"), function(z) z[[1]]) #as a string
    

    There are regex solutions with gsub that will work too but in my experiences with similar problems strsplit will be less eloquent but faster.

    I supose this regex would work as well:

    gsub("([a-z]+)([/|:])([0-9]+)", "\\1", x)
    

    In this case gsub was faster:

    Unit: microseconds
            expr    min     lq median     uq     max
    1     GSUB() 19.127 21.460 22.392 23.792 106.362
    2 STRSPLIT() 46.650 50.849 53.182 54.581 854.162
    
    0 讨论(0)
  • 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 =
    0 讨论(0)
  • 2021-01-19 03:13

    This regex seems to work. Can you use that in R?

    0 讨论(0)
  • 2021-01-19 03:21

    Something like this would do the trick in Ruby http://rubular.com/r/PzVQVIpKPq

    ^(\w+)(?:[:\/])
    

    Starting from the front of the string, grab any word characters and capture them, until you reach the non-capturing / or :

    0 讨论(0)
提交回复
热议问题