[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Strange split with parentheses in Regexp

Oliver Cromm

5/26/2005 8:40:00 PM

I just stumbled over the following effect and (trial-and error)
workaround:

p "abcxyxyxydef".split(/(xy)+/) #=> ["abc","xy","def"]
p "abcxyxyxydef".split(/(?:xy)+/) #=> ["abc","def"]

Why this difference?
--
Microsoft designed a user-friendly car:
instead of the oil, alternator, gas and engine
warning lights it has just one: "General Car Fault"
1 Answer

James Gray

5/26/2005 9:03:00 PM

0

On May 26, 2005, at 3:55 PM, Oliver Cromm wrote:

> I just stumbled over the following effect and (trial-and error)
> workaround:
>
> p "abcxyxyxydef".split(/(xy)+/) #=> ["abc","xy","def"]
> p "abcxyxyxydef".split(/(?:xy)+/) #=> ["abc","def"]
>
> Why this difference?

A capture in a split() Regexp is taken to indicate pieces of the
separator you would like returned in the resulting set.

Hope that helps.

James Edward Gray II