Solved

Ruby and Regex and POP

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

14 Experts available now in Live!

Get 1:1 Help Now