From: Robert Klemme Date: 2008-03-25T23:49:00+09:00 Subject: Re: Help parsing and extracting a num 2008/3/25, Iņaki Baz Castillo : > Hi, I'm trying to extract a num from a string. This string is in fact > some number of headers separated by \r\n or \n. for now I just need to > extract the value of "Content-Length" (case insensitive) header. Why don't you use Net::Http? IIRC this gives you individual header fields already. > For example: > > irb> headers = "Via: via1,via2,\r\n via3\r\nFrom: > from_user@domain\nContent-length: 22\r\nTo: to_user@domain\n" > > I do the following: > > irb> headers.scan(/content-length\s*:\s*([0-9]*)/i).to_s.to_i > => 22 > > In case the "Content-Length" header exist but has empty value I get 0 > with the above code (that's what I need in fact). > > Is there any way faster to do the same? any help? headers[/content-length:\s*(\d+)/i, 1].to_i Cheers robert -- use.inject do |as, often| as.you_can - without end