【发布时间】:2017-07-29 17:43:17
【问题描述】:
嘿,在 Ruby 中,如何拆分多个空格或制表符?我试过这个
2.4.0 :003 > a = "b\tc\td"
=> "b\tc\td"
2.4.0 :005 > a.strip.split(/([[:space:]][[:space:]]+|\t)/)
=> ["b", "\t", "c", "\t", "d"]
但是标签本身正在变成标记,这不是我想要的。以上应该返回
["b", "c", "d"]
【问题讨论】:
-
使用
\s+模式进行拆分功能,将返回您想要的。用法:.split(/\s+/)
标签: ruby regex string split whitespace