R gsub partial replacement wildcards

Viewed 1280

I hope this is sufficiently different related, prior posts to justify its own thread; unfortunately, they were unhelpful to me. I think my interest in partial replacement, plus use of a wildcard is thus far unique, but apologies if I simply haven't searched or read carefully enough!

Suppose I have the following string:

str <- c("FOO_1", "FOO_2", "BAR_1", "BAR_2")

I'd like to replace the 1 at the end of FOO_1 with something else, say A. I attempted to do so with both

gsub("[^F.*](1)$", "\\_A", str)

and

gsub("^F.*(1)$", "\\_BLAH", str)

but clearly neither of them worked to replace only 1, leaving the rest of FOO_ intact, while also not altering BAR_1. I have a dumb solution to the problem that simply involves a line of grep() and a line of gsub(), but I will hate myself if that's what I settle on.

1 Answers
Related