Help with RegEx

I need some help with the following:

I need to replace

content_for(:head) { stylesheet_link_tag(*args) }

with

content_for(:head_stylesheet) { stylesheet_link_tag(*args) }

using gsub_file but I do not know the proper way to do that.

Please help.

QAS WM wrote in post #1009316:

I need to
… do something …
using gsub_file

A quick google suggests that gsub_file is part of Rails.

Please ask questions about Rails on a Rails mailing list or forum. This
list is for Ruby, the programming language; Rails is an application
which happens to be written in Ruby.

I’m text agnostic when answering regex questions.

str = ‘content_for(:head) { stylesheet_link_tag(*args) }’
new_str = str.sub(’:head’, ‘:head_stylesheet’)
puts new_str

–output:–
content_for(:head_stylesheet) { stylesheet_link_tag(*args) }

7stud – wrote in post #1009404:

I’m text agnostic when answering regex questions.

str = ‘content_for(:head) { stylesheet_link_tag(*args) }’
new_str = str.sub(’:head’, ‘:head_stylesheet’)
puts new_str

–output:–
content_for(:head_stylesheet) { stylesheet_link_tag(*args) }

I usually prefer a bit more restrictive matching

irb(main):005:0> s = ‘content_for(:head) { stylesheet_link_tag(*args) }’
=> "content_for(:head) { stylesheet_link_tag(args) }"
irb(main):006:0> s.sub %r{content_for(:head(?=))}, ‘\&_stylesheet’
=> "content_for(:head_stylesheet) { stylesheet_link_tag(args) }"
irb(main):007:0> s.sub %r{content_for(:head(?=\s
))}, ‘\&_stylesheet’
=> "content_for(:head_stylesheet) { stylesheet_link_tag(args) }"
irb(main):008:0> s.sub %r{content_for(\s
:head(?=\s
))},
‘\&_stylesheet’
=> “content_for(:head_stylesheet) { stylesheet_link_tag(*args) }”

Kind regards

robert