按多个分隔符分割string
我想通过空格来分割一个string,
并且使用一个ruby命令。
-
word.split
会被空格分割; -
word.split(",")
将被分割,
; -
word.split("\'")
将被分割。
如何一次完成所有三个?
word = "Now is the,time for'all good people" word.split(/[\s,']/) => ["Now", "is", "the", "time", "for", "all", "good", "people"]
正则expression式。
"a,b'c d".split /\s|'|,/ # => ["a", "b", "c", "d"]
这是另一个:
word = "Now is the,time for'all good people" word.scan(/\w+/) # => ["Now", "is", "the", "time", "for", "all", "good", "people"]
x = "one,two, three four" new_array = x.gsub(/,|'/, " ").split