Julien Genestoux
Julien Genestoux

Reputation: 32992

Non destructive split in Ruby

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?

Upvotes: 3

Views: 555

Answers (1)

Daniel O'Hara
Daniel O'Hara

Reputation: 13438

You can do it with this simple regular expression:

"hello+world-apple+francisco-rome".scan(/[+\-]?\w+/)

Upvotes: 4

Related Questions