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
Ruby Method To Extract Emails From A String Into An Array
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