What is the best way of extracting a key value pair from a file.
For example I have an email text file which goes like so :
–
Return-Path: [email protected]
Delivered-To: [email protected]
Date: Sat, 3 May 2008 18:25:13 -0400
From: “Athar Shiraz Siddiqui” [email protected]
To: [email protected]
Subject: Testing your email checking capabilities
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
testing …
Now I would like to extract the usual From: and To: values from this
text file.
YAML seems ideal for this but the only problem is that YAML gives the
following error : ArgumentError (syntax error on line 23, col -1: `To:
[email protected]
Subject: Testing your email checking capabilities
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
testing …
‘):
c:/ruby/lib/ruby/1.8/yaml.rb:133:in load' c:/ruby/lib/ruby/1.8/yaml.rb:133:in
load’
/app/controllers/messages_controller.rb:66:in `parseMessage’;
So this is definitely a problem and I can’t use YAML.
I tried regexps but I am new to them and I can only extract the entire
line like so :
result = /^From:.*$/.match(@emailcontent) # where @email content is the
text of the email
Regexp result matchdata object gets the entire From : “…” <> line
including the from. I would like the value of “From:” (NOT including
From: meaning I would like the email address that is after the colon).
What do I use here ? In java we could do string.indexof and substring
etc. Is there something comparable? I am going through the api right now
and thinking of a way of removing the “from:” key from the returned line
; delete won’t work because it deletes all characters in intersection!
so that wont extract the value of “From:” . I am trying gsub(“From:”,
“”) as a work around for now. but any less clunking solutions would be
sweet.