如何在ruby中拆分字符串,在分割中保留空格

时间:2021-08-14 21:37:05

I have a string:

我有一个字符串:

"hello\t    World\nbla" 

I would like to split it to:

我想把它分成:

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

How would I do this in Ruby?

我将如何在Ruby中执行此操作?

2 个解决方案

#1


8  

>> "hello\t    World\nbla".scan /\S+\s*/
=> ["hello\t    ", "World\n", "bla"]

#2


2  

Hopefully this helps..

希望这有助于......

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

#1


8  

>> "hello\t    World\nbla".scan /\S+\s*/
=> ["hello\t    ", "World\n", "bla"]

#2


2  

Hopefully this helps..

希望这有助于......

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