Given a string like:
s = "G o o d\r\nDay\r\n\r\n\r\nStack\r\n\r\nOverflow\r\n"
I would like to:
Split it by (\r\n)+
, i.e. I would like to get: ["G o o d", "Day", "Stack", "Overflow"]
I tried s.split(/(\r\n)+/)
but it doesn't give me the expected result.
Why ? How could I get the expected result ?
Get the number of \r\n
in array, i.e. the expected result is: [1, 3, 2]
How would you do this ?
I use Ruby 1.9.2.
Almost, try this:
s.split /[\r\n]+/
s.scan(/[\r\n]+/).map { |e| e.size/2 }
This gives [1,3,2,1]
which is possibly the "real" answer. But otherwise, s.chomp.scan...
would give [1,3,2]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With