# Re: Question about split method

**URL:** <https://www.ruby-forum.com/t/re-question-about-split-method/130658>\
**Category:** Ruby\
**Created:** [March 1, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/re-question-about-split-method/130658 "2008-03-01T12:45:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@glenn](https://www.ruby-forum.com/u/glenn)\
**Post date:** [March 1, 2008, 12:45pm UTC](https://www.ruby-forum.com/t/re-question-about-split-method/130658/1 "2008-03-01T12:45:05Z")

</div>

Thanks for all of the tips about this question.

I actually shortened the suggestion below to:

“a.b. c! d?? e.”.scan(/.\*?[.!?]+/)

This returns all of the sentences as elements of the array and includes  
the punctuation. It works even if there are no spaces between the  
punctuation and the next sentence, and it works if there are 1 or more  
‘.’, ‘?’, or ‘!’:

[“a.”, “b.”, " c!“, " d??”, " e."]

Thanks again,

Glenn

----- Original Message ----  
From: Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Monday, February 25, 2008 7:43:45 AM  
Subject: Re: Question about split method

2008/2/25, Glenn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> The three sentences in the above string have very different meanings, but loose those meanings without the punctuation, so I’d like to keep the punctuation. I’d like a method that keeps the split characters, and returns this array:
> 
> [“This is a sentence.”, " This is a sentence!“, " This is a sentence?”]
> 
> Does such an array exist? If not, would it be possible to modify the split method to produce that result?
> 
> I’m running Ruby 1.8.6 on Windows.

Hm, you could do it with lookbehind on 1.9. On 1.8 you only have  
lookforward which gives you this:

irb(main):002:0\> “a. b.”.split /(?=.\s+)/  
=\> [“a”, “. b.”]

Not quite what you wanted. 🙂

But here’s an alternative approach which works with 1.8:

irb(main):005:0\> “a. b. c! d? e.”.scan /.\*?.!?/  
=\> ["a. ", "b. ", "c! ", "d? ", “e.”]

Kind regards

robert
