# Best Way of Extracting Key Value from File

**URL:** <https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786>\
**Category:** Rails\
**Created:** [June 18, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786 "2008-06-18T15:14:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 18, 2008, 3:14pm UTC](https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786/1 "2008-06-18T15:14:43Z")

</div>

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: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Delivered-To: removed\_email\_address@domain.invalid  
Date: Sat, 3 May 2008 18:25:13 -0400  
From: “Athar Shiraz Siddiqui” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
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:  
removed\_email\_address@domain.invalid  
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.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 18, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786/2 "2008-06-18T15:17:30Z")

</div>

If you just need to read e-mails, you can do that without bothering with  
saving them to a file. The to/from fields will be easily accessible  
then.

I ended up using an ActionMailer and the plugin linked to here:  
[http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html](http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html)

You’ll need to modify that plugin a bit, however. If its what you’re  
looking for, let me know when you get stuck with it, and I’ll try and  
help out.

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 18, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786/3 "2008-06-18T15:24:17Z")

</div>

Dan \_\_ wrote:

> If you just need to read e-mails, you can do that without bothering with  
> saving them to a file. The to/from fields will be easily accessible  
> then.
> 
> I ended up using an ActionMailer and the plugin linked to here:  
> [http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html](http://siannopollo.blogspot.com/2008/02/mail-fetcher-fetch-email-from-ruby-and.html)
> 
> You’ll need to modify that plugin a bit, however. If its what you’re  
> looking for, let me know when you get stuck with it, and I’ll try and  
> help out.

Precisely I just need to read emails from the inboxes folder/directory  
of the James Mail server. ([http://james.apache.org](http://james.apache.org))

This however looks promising. It is an SMTP server though (not pop)

---

<div class="post-metadata">

**Author:** ![ror\_arch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ror\_arch](https://www.ruby-forum.com/u/ror_arch)\
**Post date:** [June 18, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/best-way-of-extracting-key-value-from-file/140786/4 "2008-06-18T15:30:07Z")

</div>

Unfortunately, I don’t know how to read emails from an SMTP server ☹  
The plugin I sent only works with POP and IMAP. I faced this problem a  
little while ago (although I was using a POP server), and I still have  
bookmarks from when I was searching. Perhaps one of them will help you?

[http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer](http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer)  
[http://blog.craigambrose.com/past/2008/2/9/respond\_toemail\_or\_how\_to\_handle/](http://blog.craigambrose.com/past/2008/2/9/respond_toemail_or_how_to_handle/)  
[http://offtheline.net/2008/2/14/receiving-email-with-rails](http://offtheline.net/2008/2/14/receiving-email-with-rails)

Most of the examples use SMTP, but not the James Mail server you’re  
using. So I think you could use the basic idea, but you’d have to  
figure out the specific configurations yourself.
