Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non destructive split in Ruby

Tags:

string

split

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?

like image 631
Julien Genestoux Avatar asked Jul 17 '26 18:07

Julien Genestoux


1 Answers

You can do it with this simple regular expression:

"hello+world-apple+francisco-rome".scan(/[+\-]?\w+/)
like image 148
Daniel O'Hara Avatar answered Jul 19 '26 07:07

Daniel O'Hara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!