From: "Jesús Gabriel y Galán" Date: 2010-03-26T07:35:33+09:00 Subject: Re: index of string from beginning of line vs beginning of file On Thu, Mar 25, 2010 at 9:09 PM, Jesse B. wrote: > Thank you Jesus, > Your solution is much more elegant. > However it leaves a blank line where my string was. any idea how to > close that gap? > > i.e. > > opening text > --/text to delete/-- > closing text > > now renders as: > > opening text > > closing text > > and what I am going for is: > > opening text > closing text > > thanks again for all of your help! infile = File.read("input.txt") File.open("output.txt", "w") do |outfile| outfile.puts infile.split(%r{\n?--/.*?/--}m).join end Two changes: - I added an optional \n at the beggining of the matched area to cover that case, although there might be other corner cases. - I joined the array before writing it, because puts with an array prints a newline between each element. See if this works for you and test any other edge case regarding carriage returns. You can print what the split returns to see what you get in each case: infile = File.read("input.txt") File.open("output.txt", "w") do |outfile| splitted = infile.split(%r{\n?--/.*?/--}m) puts splitted.inspect outfile.puts splitted.join end Jesus.