我正在使用 Ruby 并寻找一种方法来读取包含以下文本的示例字符串:
"This is a test
file, dog cat bark
meow woof woof"
并将元素拆分为基于空格的字符数组,但将\n
数组中的值保留为单独的元素。
我知道我可以用string.split(/\n/)
得到
["this is a test", "file, dog cat bark", "meow woof woof"]
也string.split(/ /)
产生
["this", "is", "a", "test\nfile,", "dog", "cat", "bark\nmeow", "woof", "woof"]
但我正在寻找一种方法来获得:
["this", "is", "a", "test", "\n", "file,", "dog", "cat", "bark", "\n", "meow", "woof", "woof"]
有没有办法使用 Ruby 来完成这个任务?