String#split(/\s+/) vs. String#split(/(\s+)/)

Hello Rubyists,

I’m reading Ruby Cookbook.
The first chapter is about String.
One of the examples shows the differenct between String#split(/\s+/) and
String#split(/(\s+)/) without much explanation.
I understand what sub-grouping is in regex.
Bug I don’t understand what role that plays in String#split.

s = “one two three”

p s.split(/\s+/) #=> [“one”, “two”, “three”]
p s.split(/(\s+)/) #=> [“one”, " ", “two”, " ", “three”]

Could anybody explain it, please?

Thanks,
Sam

Sam K. wrote:

Hello Rubyists,

I’m reading Ruby Cookbook.
The first chapter is about String.
One of the examples shows the differenct between String#split(/\s+/) and
String#split(/(\s+)/) without much explanation.
I understand what sub-grouping is in regex.
Bug I don’t understand what role that plays in String#split.

s = “one two three”

p s.split(/\s+/) #=> [“one”, “two”, “three”]
p s.split(/(\s+)/) #=> [“one”, " ", “two”, " ", “three”]

Try this one

p s.split /((((\s+))))/

Could anybody explain it, please?

Thanks,
Sam

Hi –

On Sat, 12 Aug 2006, Sam K. wrote:

p s.split(/\s+/) #=> [“one”, “two”, “three”]
p s.split(/(\s+)/) #=> [“one”, " ", “two”, " ", “three”]

Could anybody explain it, please?

When you use (), you get the delimiter (the thing you’re splitting on)
back in the array, along with the items between the delimiters. An
example without spaces might make it clearer:

“aaaXXXbbbXXXccc”.split(/XXX/) => [“aaa”,“bbb”,“ccc”]
“aaaXXXbbbXXXccc”.split(/(XXX)/) => [“aaa”,“XXX”,“bbb”,“XXX”,“ccc”]

In your example, the delimiter is \s+ which is of variable length;
that’s why you get both " " and " " in the final array.

David

Why does using the parentheses cause the separator string/character
to be placed into the resulting array?

-ken

On 8/12/06, Eero S. [email protected] wrote:

s = “one two three”
Thanks,
Sam

Seems like all groups in the separator regex are output to the result
array.

I wonder where is it documented, except for the source itself?
(string.c, rb_str_split_m())

On 8/12/06, Jan S. [email protected] wrote:

Thanks,
Sam

Seems like all groups in the separator regex are output to the result array.

I wonder where is it documented, except for the source itself?
(string.c, rb_str_split_m())

Well the pickaxe (2nd ed.) says so:

“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. If pattern includes groups, these groups will
be included in the returned values.”

Ruby-doc.org doesn’t have that last sentence, in either the 1.8 nor
the 1.9 documentation.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs