Update
This is the final solution:
puts script.gsub(/(\^\w+)\{([^}]+)(})/, '[BEFORE]\2[AFTER]')
Although I would prefer this with fewer groups:
puts script.gsub(/\^\w+\{([^}]+)}/, '[BEFORE]\1[AFTER]')
Original answer
I need to replace the part ^foo{} with something else
Here's how to do it with gsub :
s = "^foo{bar}@Something_Else" puts s.gsub(/(.*)\^\w+\{([^}]+)}(.*)/, '\1SOMETHING ELSE\2\3')
Watch the demo
The method is the same: you save the text you want to save, and simply match the text you want to delete, and use backlinks to restore the text that you captured.
The regular expression matches:
(.*) - matches and captures as much text as possible from the very beginning in group 2\^\w+\{ - matches ^ , 1 or more word characters, {([^}]+) - matches and captures a group of 2 1 or more characters other than }} - matches }(.*) - and finally, we map and write to group 3 the rest of the line.
source share