Regex, str_extract()

Hi,

I'd like to apply str_extract() to get the part of string between the second and third underscore. Could you help me to formulate a regex pattern, please?

aaaa_bbbb_cccccc_dddd_eeee

Many thanks,

Jakub

For your example I would avoid regex, it seems a simple split by token, and then picking a fixed part by position.

str_split(rep("aaaa_bbbb_cccccc_dddd_eeee",2),pattern = "_",n=Inf,simplify = TRUE)[,3]
1 Like

only if you're sure that you got four underlines in the target string, simply do:

your_string %>% str_extract("(?<=_).+(?=_)") %>% str_extract("(?<=_).+(?=_)")

otherwise, do:

your_string %>% str_extract_all("(?<=_).+?(?=_)") %>% unlist %>% .[2]

or more simply:

your_string %>% str_split("_") %>% unlist %>% .[3]

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.