# String#split "feature"

**URL:** <https://www.ruby-forum.com/t/string-split-feature/160006>\
**Category:** Ruby\
**Created:** [February 10, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/string-split-feature/160006 "2009-02-10T21:21:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 10, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/string-split-feature/160006/1 "2009-02-10T21:21:58Z")

</div>

Just got bitten by some functionality of split w/ grouped patterns:

(in ruby 1.8.6…)

> > “foo bar baz”.split(/bar/)  
> > =\> ["foo “, " baz”]
> 
> > “foo bar baz”.split(/(bar)/)  
> > =\> ["foo “, “bar”, " baz”]

The 1.8 docs are silent on this, but the 1.9 docs at least say  
"If pattern contains groups, the respective matches will be returned in  
the  
array as well. "

This “feature” surprised the heck out of me… Anyone know if this  
functionality is intentional (from the enduser standpoint), or rather a  
byproduct of implementation details?

Anyhoo, if anyone else gets weird behavior for string split including  
the  
pattern, or with groups in the pattern, maybe you’ll remember this or  
maybe  
google will tell you about this.

Thanks,  
-Gary

---

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 10, 2009, 9:39pm UTC](https://www.ruby-forum.com/t/string-split-feature/160006/2 "2009-02-10T21:39:11Z")

</div>

Gary Y. wrote:

> The 1.8 docs are silent on this, but the 1.9 docs at least say  
> "If pattern contains groups, the respective matches will be returned in  
> the  
> array as well. "

# pickaxe2 p.619:

# split … … If pattern includes groups, these groups will be included in the returned values. …

> This “feature” surprised the heck out of me… Anyone know if this  
> functionality is intentional (from the enduser standpoint), or rather a  
> byproduct of implementation details?

It can be a really handy feature. python’s regex split() function works  
the same way.

> Anyhoo, if anyone else gets weird behavior for string split including  
> the  
> pattern, or with groups in the pattern, maybe you’ll remember this or  
> maybe  
> google will tell you about this.
> 
> Thanks,  
> -Gary

---

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 10, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/string-split-feature/160006/3 "2009-02-10T21:48:26Z")

</div>

On Feb 10, 2009, at 3:38 PM, 7stud – wrote:

> # ===== split … … If pattern includes groups, these groups will be included in the returned values. …

Interestingly, this feature is described in String#scan, but the ri  
docs for my Apple-provided ruby do not have the sentence for  
String#split.

$ ruby -v  
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

-Rob

> > Anyhoo, if anyone else gets weird behavior for string split including  
> > the  
> > pattern, or with groups in the pattern, maybe you’ll remember this or  
> > maybe  
> > google will tell you about this.
> > 
> > Thanks,  
> > -Gary

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 10, 2009, 11:15pm UTC](https://www.ruby-forum.com/t/string-split-feature/160006/4 "2009-02-10T23:15:30Z")

</div>

Le 10 février 2009 à 21:20, Gary Y. a écrit :

> This “feature” surprised the heck out of me… Anyone know if this  
> functionality is intentional (from the enduser standpoint), or rather a  
> byproduct of implementation details?
> 
> Anyhoo, if anyone else gets weird behavior for string split including the  
> pattern, or with groups in the pattern, maybe you’ll remember this or maybe  
> google will tell you about this.

It’s quite useful. Note that you can always use non-capturing groups  
(the (?:expr) construct) if you don’t want them :

> > “one, two and three”.split(/\s\*(and|or|,)?\s+/)  
> > =\> [“one”, “,”, “two”, “and”, “three”]

> > “one, two and three”.split(/\s\*(?:and|or|,)?\s+/)  
> > =\> [“one”, “two”, “three”]

Fred
