Do this for all named capture group
stringi::stri_replace_all_regex("foo_bar", "(?<name>.*)_(?<value>.*)", "${value}")
#> [1] "bar"
str_extract_named_capture_groups <- function(x, pattern) {
groups <- stringi::stri_extract_all_regex(pattern, "(?<=\\(\\?<).*?(?=>)")[[1]]
replacements <- paste0("${", groups, "}")
names(replacements) <- groups
purrr::map_dfc(replacements, ~ stringi::stri_replace_first_regex(x, pattern, .))
}
str_extract_named_capture_groups(c("foo_bar", "a_b"), "(?<name>.*)_(?<value>.*)")
#> # A tibble: 2 x 2
#> name value
#> <chr> <chr>
#> 1 foo bar
#> 2 a b
Created on 2019-01-27 by the reprex package (v0.2.1)