I'm not sure if someone's already answered this, but...

putting parentheses around things groups them... and it's treated as  
though it's a single regexp...

so:
/\s*/ means match a space, zero or more times to the extent of the  
contiguous spaces...

but
/(\s)*/ means "match a space, zero or more times to the extent of  
THIS CONTIGUOUS MATCH. It first matches zero spaces, then the limit  
of the zero spaces is ... (funnily enough) zero spaces, so it doesn't  
go any further. You don't want to use parentheses.

There have been whole books written on regular expressions. If you're  
going to use them well, they're worth reading, I'd suggest.

Julian.

On 18/08/2005, at 3:31 PM, Mike Meng wrote:

> Hi,
>   I'm new to Ruby and reading 'Programming Ruby 2/e' now. I  
> encountered
> a tricky problem while reading chapter 5, 'String" section. Here is  
> the
> problem:
>
> # code
> line = '/jazz/j00319.mp3 | 2:58 | Louis Armstrong | Wonderful World'
> file, duration, artist, title = line.chomp.split(/\s*\|\s*/)
> # code end
>
> Run the code we get:
> file=='/jazz/j00319.mp3'
> duration=='2:58'
> artist=='Louis Armstrong'
> title=='Wonderful World'
>
> While if I change the regex pattern in 'split' to /(\s)*\|(\s)*/,
> that is,
> # code
> line = '/jazz/j00319.mp3 | 2:58 | Louis Armstrong | Wonderful World'
> file, duration, artist, title = line.chomp.split(/(\s)*\|(\s)*/)
> # code end
>
> We get:
> file=='/jazz/j00319.mp3'
> duration==' '
> artist==' '
> title=='2:58'
>
> What makes the differece? Any comments are appreciated.
>
>
>