From: dblack@... Date: 2006-10-18T09:11:45+09:00 Subject: Re: Splitting a sentence with delimiter preserved Hi -- On Wed, 18 Oct 2006, 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!!!."] Also, in 1.9, with oniguruma, you can do: string.split(/(?<=\.)\s+/) (negative lookbehind). David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org