Your replacement string is not a regex, so \d
has no special meaning, but is just a literal string. You need to specify a group in your regex, and then you can use the captured group in your replacement string:
FileUtils.cp(file, file.gsub(/#{text}--(\d\d)/, "#{text}--\\1Bass "))
The parenthesis in the regex form the group, which can be used (by number) in the replacement string: \1
for the first group, \2
for the second, etc. \0
refers to the entire regex match.
Update
Replaced gsub!()
with gsub()
and escaped the backslash in the replacement string (to treat \1
as the capture group, not a literal character... Doh!).