Hi --

On Sat, 12 Aug 2006, Sam Kong 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"]
>
>
> 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

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
http://www.manning.com/black     => book, Ruby for Rails
http://www.rubycentral.org       => Ruby Central, Inc.