I want to replace a string in a file who is delimited by 2 other string.
My problem is to find the start and the end string delimiter and replace
the content between both.
On Tue, Oct 03, 2006 at 04:55:44AM +0900, S?bastien Maurette wrote:
Hello,
I want to replace a string in a file who is delimited by 2 other string.
My problem is to find the start and the end string delimiter and replace
the content between both.
In this example i’want to replace the string /startdel2/ text2 text2
text2 /enddel2/
by
/startdel2/ hello /enddel2/
left = “/startdel2/”
right = “/enddel2/”
p str.gsub(/#{Regexp.quote(left)}(.*?)#{Regexp.quote(right)}/m,
“#{left}hello#{right}”)
You need the /m as there might be newlines in the match, and you need
the Regexp.quote as your strings contain characters which lose their
usual meaning in a regexp (*).