Split string

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

···

On 2013-07-04, at 8:59 AM, xcoder Blue_fox <lists@ruby-forum.com> 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/\.

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

First reaction is to tell you to quit being lazy and go read the docs. Your
example practically in the docs, and you even know the name of the class
and the method. http://rdoc.info/stdlib/core/String:split

Then I realized there are two \r\n after the first line, in which case
maybe your difficulty is that you don't know how to construct the regex
that won't give you an empty line: /\r\n\r\n|\r\n/

But then I realized that there are not two \r\n after the first line of an
http request: HTTP/1.1: Request
your query string is wrong, and I can't figure out what you're
actually
trying to do.

···

On Thu, Jul 4, 2013 at 7:59 AM, xcoder Blue_fox <lists@ruby-forum.com>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/\.

I am pretty sure you'll find that functionality in Webrick.

Cheers

robert

···

On Thu, Jul 4, 2013 at 2:59 PM, xcoder Blue_fox <lists@ruby-forum.com>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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

so your query is like,

query = "Get /index.html HTTP/1.0\r\n\r\nHost:
www.example.com\r\nConnection: Close\r\n\r\n"

Now you do a simple regular expression to split the string like,

      split_string = query.split(/\r\n/)

This will give you an array like this,

     split_string = ["Get /index.html HTTP/1.0", "", "Host:
                     www.example.com", "Connection: Close"

If you want to delete the null value from the above array just do,

     split_string.delete("")

···

--
Posted via http://www.ruby-forum.com/.

thanks guys for responding .

···

--
Posted via http://www.ruby-forum.com/.