randomor
randomor

Reputation: 5673

How do I parse this string in ruby?

Could someone tell me how to parse this string

From: Dela([email protected]) To: Roger([email protected]) Date: Monday, Oct 11 Subject: about emma

and store it into a hash like:

{:from=> "Dela([email protected])", :to=>"Roger([email protected])", :date=>"Monday, Oct 11", :subject=>"about emma"}

Upvotes: 1

Views: 107

Answers (1)

sawa
sawa

Reputation: 168269

str = "From: Dela([email protected]) To: Roger([email protected]) Date: Monday, Oct 11 Subject: about emma"

Hash[str.scan(/([a-zA-Z]+):\s+(.*?)(?=\z|[a-zA-Z]+:)/).map{|k, v| [k.downcase.to_sym, v]}]

Upvotes: 6

Related Questions