re = /(^reflectance_\d_)(\S*)$/m
str = 'reflectance_7_myd21a1_emissivity_29
reflectance_7_myd21a1_emissivity_29_savitzkygolay
reflectance_7_myd21a1_emissivity_31
reflectance_7_myd21a1_emissivity_31_savitzkygolay
reflectance_3_myd21a1_emissivity_32
reflectance_2_myd21a1_emissivity_32_savitzkygolay
reflectance_1_mcd43a3_albedo_bsa_band1
reflectance_3_mcd43a3_albedo_bsa_band2
reflectance_7_mcd43a3_albedo_bsa_nir
reflectance_7_mcd43a3_albedo_bsa_shortwave
reflectance_7_mcd43a3_albedo_bsa_vis
reflectance_7_mcd43a3_albedo_wsa_band1
reflectance_7_mcd43a3_albedo_wsa_band2
reflectance_7_mcd43a3_albedo_wsa_nir
reflectance_7_mcd43a3_albedo_wsa_shortwave
reflectance_7_mcd43a3_albedo_wsa_vis'
subst = '($1)vs_$2'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html