I'm sure this is a silly question, I have a couple of strings such as data_PB_Belf.csv
and I need to exctract only PB_Belf
(and so on). How can I exctract everything after the first _ up to . (preferably using stringr
) ?
data
[1] "data_PB_Belf.csv" "data_PB_NI.csv" ...
str_replace(data[1], "^[^_]+_([^_]+)_.*", "\\1") ## the closer I got, it returns "PB"
- I tried to adapt the code from here, but I wasn't able to. I'm sure that there's a way to use
str_replace()
orstr_sub()
orstr_extract()
, I just can't get the right Regex. Thanks in advance!