How to split a string in ruby ​​while keeping spaces in a split

I have a line:

"hello\t World\nbla" 

I would like to break it into:

 ["hello\t ", "World\n", "bla"] 

How can I do this in Ruby?

+6
ruby
source share
2 answers
 >> "hello\t World\nbla".scan /\S+\s*/ => ["hello\t ", "World\n", "bla"] 
+8
source share

Hope this helps ...

 >> "hello\t World\nbla".scan(/\w+\s*/) => ["hello\t ", "World\n", "bla"] 
+2
source share

All Articles