将字符串拆分为数组

时间:2014-09-16 06:07:09

标签: ruby string ruby-on-rails-3

输入:

2014-09-16T05:59:43,274     17898026714983488   127.0.0.1   /banner b=123&c=test&p=45   host:test.google.com|x-forwarded-for:182.65.228.222|accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8|user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36|accept-language:en-US,en;q=0.8|cookie:_ga=GA1.2.1550976853.1405927868; __gads=ID=058659026f235a09:T=1409198510:S=ALNI_MambZn0wB2dVEwa4AbsP2Mok728qQ

预期值:

 [2014-09-16T05:59:43,274   ,
    17898026714983488,
    127.0.0.1,
    /banner,
    b=123&c=test&p=45 ,
    host:test.google.co,
    x-forwarded-for:182.65.228.22,
    accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.,
    user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916153 fari/537.36|accept-language:en-US,en;q=0.,
    cookie:_ga=GA1.2.1550976853.05927868;__gads=ID=058659026f235a09:T=1409198510:S=ALNI_MambZn0wB2dVEwa4AbsP2Mok728q]

有没有办法将每个值存储到ruby中的数组中?

2 个答案:

答案 0 :(得分:1)

至少对于给定的示例,这将是,但您可能需要调整以获得更稳定的结果:

split('|').each_with_index.map {|element, index| index == 0 ? element.split : element }.flatten

答案 1 :(得分:0)

将方法.split()用于字符串