I have a list of strings and I need to change those of them being followed by a certain suffix to being preceded by a certain prefix. I was using gsub and back referencing the the parenthesized subexpression in the pattern argument in the replacement.
The problem is that the function pushes the backreference to the beginning of the output string
> gsub("([:alnum:]*).suff", "\\1", "string.suff")
[1] "string"
> gsub("([:alnum:]*).suff", "pre_\\1", "string.suff")
[1] "stringpre_"
any idea, possibly not involving a workaround via a combination of endsWith
, paste
and ifelse
?