Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby Method To Extract Emails From A String Into An Array

DZone's Guide to

Ruby Method To Extract Emails From A String Into An Array

·
Free Resource
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" }


Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}