有损分裂和连接表达式

时间:2015-07-14 12:09:13

标签: ruby regex string-split

我正在使用currentState方法,但在split(/ /)之后,结束的空白区域消失了。

匹配的字符串是:

join

从split(/ /)返回的数组是:

"word word  word  "

我期待这个:

["word", " ", "word", " ", " ", "word"]

1 个答案:

答案 0 :(得分:2)

这应该对你有用

str.scan(/\w+| /) 

这是一个例子

  

2.2.1:003> "单词单词" .scan(/ \ w + | /)
  => ["字"," "," word"," "," "," word"," "]