1

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"}

1 Answer 1

6

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]}]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.