From: Robert Klemme Date: 2006-10-18T17:35:12+09:00 Subject: Re: Splitting a sentence with delimiter preserved On 17.10.2006 22:37, Gavin Kistner wrote: > From: Ajithkumar Warrier [mailto:a.varier@gmail.com] >> I am a newbie and the answer to this might be too simple. >> How do I improve the example below and reduce the number of passes >> over the string? >> >> string = "This is an example string. The purpose is to save the >> delimiter during split. Does this work. Great!!!." >> >> re = /(\.\s+)(\D)/ >> string.gsub!(re,'\1'+'#'+'\2') >> b = string.split('#') >> puts b > > p string.scan( /\w[^.!?]+\S+/ ) > > #=> ["This is an example string.", "The purpose is to save the delimiter > during split.", "Does this work.", "Great!!!."] >> string = "This is an example string. The purpose is to save the delimiter during split. Does this work. Great!!!." >> string = string + " It costs 0.1 dollars." => "This is an example string. The purpose is to save the\ndelimiter during split. Does this work. Great!!!. It costs 0.1 dollars." >> string.scan( /\w[^.!?]+\S+/ ) => ["This is an example string.", "The purpose is to save the\ndelimiter during split.", "Does this work.", "Great!!!.", "It costs 0.1", "do llars."] Hm... robert