regex - Is there an equivalent of "&" in R's regular expressions for backrefencing entire match -


when use vim, use & backreference entire match within substitutions. example, following replaces instances of "foo" "foobar":

%s/foo/&bar/g 

the benefit here laziness: don't have type parenthesis in match , have type 1 character instead of 2 backreference in substitution. perhaps more importantly, don't have figure out backrefrences while i'm typing match, reducing cognitive load.

is there equivalent & i'm using in vim within r's regular expressions (maybe using perl = t argument)?

the answer no base r sub/gsub functions, see this reference:

there no replacement text token overall match. place entire regex in capturing group , use \1 insert whole regex match.

however, in stringr, can use \0:

> library(stringr) > str_replace_all("123 456", "\\d+", "start-\\0-end") [1] "start-123-end start-456-end" 

Comments