将String拆分为值数组

时间:2014-09-08 09:23:13

标签: ruby string split

输入字符串: -

str = '"2014-09-04 21:12:05" 5469687123030383463 192.168.1.2 4 7879 0 43 "www.test.com/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36" 404 123 F 21549 50 0 - Test1

当我用空格分开时: -

str.split(' ')

 => ["\"2014-09-04", "21:12:05\"", "5469687123030383463", "192.168.1.2", "4", "7879", "0", "43", "\"www.test.com/\"", "\"Mozilla/5.0", "(X11;", "Linux", "x86_64)", "AppleWebKit/537.36", "(KHTML,", "like", "Gecko)", "Chrome/35.0.1916.153", "Safari/537.36\"", "404", "123", "F", "21549", "50", "0", "-", "Test1"]

预期: -

str =  ["2014-09-04 21:12:05", "5469687123030383463", "192.168.1.2", "4", "7879", "0", "43", "www.test.com/", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", "404", "123", "F", "21549", "50", "0", "-", "Test1"] 

我如何通过ruby实现

2 个答案:

答案 0 :(得分:2)

你的字符串与UNIX shell参数非常相似,我只使用Shellwords::shellsplit。请注意,这会删除引号,但您真的需要它们吗?

require 'shellwords'
Shellwords::shellsplit(str)
#=> ["2014-09-04 21:12:05", "5469687123030383463", ..., "-", "Test1"]

答案 1 :(得分:2)

您可以在正则表达式中单独涵盖这两种情况,并使用|

加入它们
str.scan(/"[^"]+"|[^ ]+/)