Skip to content

Instantly share code, notes, and snippets.

@streetcarmonkey
Last active August 29, 2015 14:12
Show Gist options
  • Save streetcarmonkey/ac71c4487ca81aa3dbd2 to your computer and use it in GitHub Desktop.
Save streetcarmonkey/ac71c4487ca81aa3dbd2 to your computer and use it in GitHub Desktop.
Ruby script to extract email addresses from a string using a regular expression.
@string = '<p><a href="http://plop.com">plop</a><br /><a mailto:ray@plop.com>ray@plop.com</a><br />cathy@plop.com.au</p>'
RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
def email_extract(doc = @string)
puts "Attempting to extract emails from a string"
emails = Array.new
doc.downcase.scan(RE).each do | email | emails.push email end
emails.uniq!
return emails
end
emails = email_extract #'<p><a href="http://notplop.com">not plop</a><br /><a mailto:raymond@notplop.com>ray@notplop.com</a><br />cathy@notplop.com.au</p>'
puts emails.size
puts emails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment