How can I use multiple backreferences in a function to produce the replacement in stringr
functions, for example, in stringr::str_replace()
?
An example: suppose I want the replacement to be rounded to a whole number and concatenated into one string (this particular function is just an example, the important thing is that it accepts > 1 backreference)
I have tried some variations on the following without success
round_concat <- function(x, y) { paste(round(as.numeric(x),0), round(as.numeric(y, 0)))}
library(stringr)
"ABC 23.3 text 105.43 more text" %>% str_replace_all(., "(\\d+)(\\.)(\\d+)", round_concat("\\1", "\\2"))
Note: I have looked for similar functionality in functions like base::gsub
(see here) but without luck