I want to replace the last number in a string using
S <- "abcd2efghi2.txt"
gsub("?<=[\d+])\b", "", S)
You can achieve that with a default TRE engine using the following regex:
Replace with the
\d+- 1 or more digits
(\D*)- Capturing group 1: any 0+ non-digit symbols
$- end of string
\1- a backreference to the Group 1 value (so as to restore the text matched and consumed with the
See the regex demo.
sub("\\d+(\\D*)$", "\\1", S) ## =>  "abcd2efghi.txt"