# Splitting a sentence with delimiter preserved

**URL:** <https://www.ruby-forum.com/t/splitting-a-sentence-with-delimiter-preserved/72538>\
**Category:** Ruby\
**Created:** [October 17, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/splitting-a-sentence-with-delimiter-preserved/72538 "2006-10-17T22:26:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajithkumar\_Warrier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@Ajithkumar\_Warrier](https://www.ruby-forum.com/u/Ajithkumar_Warrier)\
**Post date:** [October 17, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/splitting-a-sentence-with-delimiter-preserved/72538/1 "2006-10-17T22:26:36Z")

</div>

Hi,

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

Thanks  
Ajith
