From: Mike Stok Date: 2013-07-04T22:45:15+09:00 Subject: Re: Split string On 2013-07-04, at 8:59 AM, xcoder Blue_fox wrote: > hey Guys i need to split this query to string array how contains three > strings : > query = "Get /index.html HTTP/1.0\r\n\r\n" > query += "Host: www.example.com\r\n" > query += "Connection: Close\r\n\r\n" > i want to get result like > array[0] >>> Get /index.html HTTP/1.0 > array[1] >>> Host: www.example.com > array[2] >>> Connection: Close > > -- > Posted via http://www.ruby-forum.com/. What have you tried so far, and what are you trying to do? It *looks like* you're dealing with some kind of HTTP request with an extra \r\n between the Get line (method tokens in HTTP/1.1 are case sensitive). To simply achieve your end as stated you might do: ratdog:~ mike$ pry [1] pry(main)> query = "Get /index.html HTTP/1.0\r\n\r\n" => "Get /index.html HTTP/1.0\r\n\r\n" [2] pry(main)> query += "Host: www.example.com\r\n" => "Get /index.html HTTP/1.0\r\n\r\nHost: www.example.com\r\n" [3] pry(main)> query += "Connection: Close\r\n\r\n" => "Get /index.html HTTP/1.0\r\n\r\nHost: www.example.com\r\nConnection: Close\r\n\r\n" [4] pry(main)> query.split(/(?:\r\n)+/) => ["Get /index.html HTTP/1.0", "Host: www.example.com", "Connection: Close"] But if you care about multiple \r\n sequences, which are used to separate the header from the body of an HTTP request, you should think about what you want to do with malformed input. If you're doing this for your own education and enlightenment then it is a good exercise, if you want to write an application then you might want to look for gems which can process HTTP requests. Hope this helps, Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.