str <- c("FOO_1", "FOO_2", "BAR_1", "BAR_2")sub("^(FOO.*)_1$", "\\1_A", str)
Standard input is empty
[1] "FOO_A" "FOO_2" "BAR_1" "BAR_2"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!