tags:

views:

54

answers:

1

I want to split the string "hello+world-apple+francisco-rome", into ["hello", "+world", "-apple", "+francisco", "-rome"].

String::split actually loses the splitting element. Anyone can do that?

+3  A: 

You can do it with this simple regular expression:

"hello+world-apple+francisco-rome".scan(/[+\-]?\w+/)
floatless
Beautiful! Thanks.
Julien Genestoux
@Julien Genestoux: Here's a neat little trick: if you put the `-` first in the character class, then you don't need to escape it: `/[-+]/`. (Also works for `]`, I believe.)
Jörg W Mittag