From: "Jesse B." Date: 2010-03-27T00:04:21+09:00 Subject: Re: index of string from beginning of line vs beginning of file Thank you again for your help. I am finding that spaces before the "comment" cause it not to work. this works as expected: opening text --/text to delete/-- closing text however this (one space before "comment") opening text --/text to delete/-- closing text outputs a newline opening text closing text The command line output for the different cases looks like: C:\Ruby\bin\file_write>ruby jesus.rb ["opening text", "\nclosing text\n\n\n"] C:\Ruby\bin\file_write>ruby jesus.rb ["opening text\n ", "\nclosing text\n\n\n"] as you can see, there is the \n after opening text. The three \n at the end were present in the input.txt, so no problem. thank you for your time. Jes炭s Gabriel y Gal叩n wrote: > On Thu, Mar 25, 2010 at 9:09 PM, Jesse B. wrote: >> >> >> 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. -- Posted via http://www.ruby-forum.com/.