How to do str_extract with base R?

后端 未结 3 1310
忘了有多久
忘了有多久 2021-01-02 07:19

I am balancing several versions of R and want to change my R libraries loaded depending on which R and which operating system I\'m using. As such, I want to stick with base

3条回答
  •  感动是毒
    2021-01-02 08:19

    You could do

    txt <- c("foo release 123", "bar release", "foo release 123 bar release 123")
    pattern <- "release ([0-9]+)"
    stringr::str_extract(txt, pattern)
    # [1] "release 123" NA            "release 123"
    sapply(regmatches(txt, regexec(pattern, txt)), "[", 1)
    # [1] "release 123" NA            "release 123"
    

提交回复
热议问题