Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajib/901168 to your computer and use it in GitHub Desktop.
Save rajib/901168 to your computer and use it in GitHub Desktop.
ruby method to extract emails from a string into an array
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" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment