rregexgsub

How to extract string between other string and number in R?


I have a vector:

a <- c("{.package:base73","for.package:base476",":.package:base9","print.package:graphics834")

and I need to extract string between package: and the first number.

So the output should be: c("base", "base", "base", "graphics")

I tried this but it doesn't work as i wish:

b <- "{.package:base71"
gsub(".*package:(.+)[1-9]+", "\\1", b)

Edit: I want to use only base package*


Solution

  • Just a minimal change to code of OP,

    gsub(".*package:|[1-9]+", "\\1", a)
    
    #[1] "base"     "base"     "base"     "graphics"