Solved

Ruby and Regex and POP

Posted on 2009-05-05
5
357 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now