Nondestructive Split in Ruby

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

String::splitactually loses the cleavage element. Can anyone do this?

+3
source share
1 answer

You can do this with this simple regular expression:

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

Source: https://habr.com/ru/post/1757262/


All Articles