# Problem with regexp split

**URL:** <https://www.ruby-forum.com/t/problem-with-regexp-split/184376>\
**Category:** Rails\
**Created:** [March 1, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376 "2010-03-01T18:51:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/1 "2010-03-01T18:51:54Z")

</div>

I am trying to split some text into an array seperated by one or more

Here is some test code:

s = “one  
two  
  
three  
  
  
four”  
p s.split(/(  
)+/);

it should split into [“one”,“two”,“three”,“four”] because the /  
(  
)+/ pattern should use one or more   
 as the pattern to split  
around

but it does this  
[“one”, “  
”, “two”, “  
”, “three”]

Why does it do this and what split could I use to get it to work?

Note:, I know that I could just fix it by removeing the   
 lines  
after it is done from the array, but it seems that the regular  
expression in split should work.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/2 "2010-03-01T19:01:52Z")

</div>

On Mar 1, 2010, at 9:50 AM, removed\_email\_address@domain.invalid wrote:

> around
> 
> but it does this  
> [“one”, “  
> ”, “two”, “  
> ”, “three”]
> 
> Why does it do this and what split could I use to get it to work?
> 
> Note:, I know that I could just fix it by removeing the   
> lines  
> after it is done from the array, but it seems that the regular  
> expression in split should work.

Interesting. Docs say:

If pattern is a String, then its contents are used as the delimiter  
when splitting str. If pattern is a single space, str is split on  
whitespace, with leading whitespace and runs of contiguous whitespace  
characters ignored.

If pattern is a Regexp, str is divided where the pattern matches.  
Whenever the pattern matches a zero-length string, str is split into  
individual characters.

Which seems to be saying exactly what you are are describing. If a  
regexp is used the match isn’t “eaten”, but simply divided on.

You could split it on “  
” and then remove any blank elements… not  
sure if that’s any better than your alternative approach though.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/3 "2010-03-01T19:13:34Z")

</div>

yea, I have been using reg exp and ruby for years. and this is a  
puzzle.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 7:18pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/4 "2010-03-01T19:18:50Z")

</div>

Also does not behave with this code:

s = “onexytwoxyxythreexyxyxyfour”  
p s.split(/(xy)+/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/5 "2010-03-01T23:18:52Z")

</div>

array.compact.reject { |i| i.nil? or i.empty? } seemed to leave some  
unwanted elements, at least on my Ruby 1.8.6.

But array.delete\_if { |i| i.nil? or i.empty? } worked as expected on  
my machine.

HTH,  
Richard

array.delete\_if { |i| i.nil? or i.empty? }

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 11:56pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/6 "2010-03-01T23:56:19Z")

</div>

On Mon, Mar 1, 2010 at 9:50 AM, removed\_email\_address@domain.invalid \<  
removed\_email\_address@domain.invalid\> wrote:

> around

Gerry, you can do the following:

p s.gsub(/  
/, " " ).split

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 1, 2010, 8:19pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/7 "2010-03-01T20:19:54Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Also does not behave with this code:
> 
> s = “onexytwoxyxythreexyxyxyfour”  
> p s.split(/(xy)+/)

Try this:

s = “one  
two  
  
three  
  
  
four”  
array = s.split(’  
’)  
array.compact.reject { |i| i.nil? or i.empty? }

This will produce:

[‘one’, ‘two’, ‘three’, ‘four’]

Regards,

Atc.,  
Kirk Patrick

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 2, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/8 "2010-03-02T19:47:32Z")

</div>

On Mar 1, 1:00 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Why does it do this and what split could I use to get it to work?  
> > characters ignored.
> 
> If pattern is a Regexp, str is divided where the pattern matches.  
> Whenever the pattern matches a zero-length string, str is split into  
> individual characters.
> 
> Which seems to be saying exactly what you are are describing. If a  
> regexp is used the match isn’t “eaten”, but simply divided on.
> 
> You could split it on “  
> ” and then remove any blank elements… not  
> sure if that’s any better than your alternative approach though.

The trick here is a feature inherited from Perl - groups (in parens)  
in the regexp cause the delimiters to be included. This works like  
you’d expect:

s.split(/(?:  
)+/)

the ?: modifier tells the parens to group without providing a backref.

–Matt J.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 2, 2010, 2:13am UTC](https://www.ruby-forum.com/t/problem-with-regexp-split/184376/9 "2010-03-02T02:13:45Z")

</div>

RichardOnRails wrote:

> array.compact.reject { |i| i.nil? or i.empty? } seemed to leave some  
> unwanted elements, at least on my Ruby 1.8.6.
> 
> But array.delete\_if { |i| i.nil? or i.empty? } worked as expected on  
> my machine.
> 
> HTH,  
> Richard
> 
> array.delete\_if { |i| i.nil? or i.empty? }

My Ruby version is 1.8.7  
But the more important is the problem solved. =P
