Forum: Ruby String#split "feature"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aba50314ff6896ee0d601a81fc8e5f20?d=identicon&s=25 Gary Yngve (Guest)
on 2009-02-10 21:21
(Received via mailing list)
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-10 21:39
Gary Yngve 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-02-10 21:48
(Received via mailing list)
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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-02-10 23:15
(Received via mailing list)
Le 10 février 2009 à 21:20, Gary Yngve 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
This topic is locked and can not be replied to.