DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Method To Extract Emails From A String Into An Array

11.14.2008
| 6749 views |
  • submit to reddit
        self.explain


def extract_emails_to_array(txt)
  reg = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i
  txt.scan(reg).uniq
end


input = IO.readlines("in.txt")
output = File.new("mails.txt",  "w+") 


result_array = extract_emails_to_array(input.join(" ")).sort
puts result_array.size  #optional

result_array.each{|s| output << s.to_s+"\n" }


    

Comments

Snippets Manager replied on Wed, 2009/04/01 - 6:11am

If there any method to add to favorate?