Solved

Ruby and Regex and POP

Posted on 2009-05-05
5
360 Views
Last Modified: 2013-11-13
I have 2 questions:

1.) -------------------
How to write a string.split command that will take the following line and return an array of 2 email addresses:

To: "Stuart Smith" <stuart@email.com>, "Community" <comm@email.com>


2.)--------------------
I built a pop3 reader using something very similar to below however, I am looking to store the actual attachment as the mime-type itself like a PDF.  Any help?

   



POP3.start('pop.example.com', 110,
               'YourAccount, 'YourPassword') do |pop|
      n = 1
      pop.mails.each do |popmail|
        File.open("inbox/#{n}", 'w') do |f|
          f.write popmail.pop
        end
        popmail.delete
        n += 1
      end
    end

Open in new window

0
Comment
Question by:bobbit31
  • 4
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 24302335
I can answer 1) first

email_addresses = '"Stuart Smith" <stuart@email.com>, "Community" <comm@email.com>'

emails = email_addresses.split(/,/).collect{|item| item.gsub(/^.*<([^>]+)>.*$/, '\1')}

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24302360
1) is a simple split and the collect generates a new array from executing the block on each item of the array generated by the split
the block is a simple regex substitution

2) I am not sure I understand the second part of your question actually
Do you want to safe the attachment and give it an extension based on the mime type?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24572385
sorry bobbit I can't agree to your deletion
- I answered the first half of your question, you should award that at least
- I asked for clarification for the second part, you never gave that clarification
0
 
LVL 18

Author Closing Comment

by:bobbit31
ID: 31577917
sorry, i don't recall trying to delete this. i agree you deserve points.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24574288
no problem,
cheers
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Attempting to install LESS on Windows 7 with Ruby Installer 11 749
Is Ruby On Rails relevant 3 510
Rails URL Rewrite 1 418
AES-256-CBC Encryption between Ruby and PHP 5 498
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question