From: mathew Date: 2005-07-30T05:06:02+09:00 Subject: Re: Sed -> Ruby : .. and ... Keith Fahlgren wrote: > On Thursday 28 July 2005 5:46 pm, mathew wrote: >>working_file.gsub!(/(?=BEGIN RANGE)(.*?)(?=END RANGE)/m) {|| >> $1.gsub(/MATCH/m, 'REPLACEMENT') >>} > > Matthew pointed out an easier way to get around it but I had rejected > that method from the start because I wanted to be able to nest ranges > (like I can in sed). Oh, well, in that case, let's make my 3 lines into an iterator: class String # Execute a code block on the substring starting with startstring and # ending with endstring, and return the result def in_range(startstring, endstring) return gsub(/(?=#{startstring})(.*?)(?=#{endstring})/m) {|| yield($1) } end end Example usage: puts data.in_range('BEGIN RANGE', 'END RANGE') {|subdata| subdata.in_range('NESTED RANGE', 'END OF NESTED RANGE') {|x| x.gsub('MATCH', 'REPLACEMENT') } } mathew -- WE HAVE TACOS