Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll probably want to use a <a href="http://www.ruby-doc.org/core-1.9.3/Regexp.html" rel="nofollow">regular expression</a>. They'll allow you to extract the email addresses, regardless of their context within a document. Here is a little test-driven example to get you started:</p> <pre><code>require "minitest/spec" require "minitest/autorun" module Extractor EMAIL_REGEX = /[\w]+@[\w]+\.[\w]+/ def self.emails(document) (matches = document.scan(EMAIL_REGEX)).any? ? matches : false end end describe "Extractor" do it 'should extract an email address from plaintext' do emails = Extractor.emails("email@example.com") emails.must_include "email@example.com" end it 'should extract multiple email addresses from plaintext' do emails = Extractor.emails("email@example.com and email2@example2.com") emails.must_include "email@example.com", "email2@example2.com" end it 'should extract an email address from the href attribute of an anchor' do emails = Extractor.emails("&lt;a href='mailto:email3@example3.com'&gt;Email!&lt;/a&gt;") emails.must_include "email3@example3.com" end it 'should extract multiple email addresses from both plaintext and within HTML' do emails = Extractor.emails("my@email.com OR &lt;a href='mailto:email4@example4.com'&gt;Email!&lt;/a&gt;") emails.must_include "email4@example4.com", "my@email.com" end it 'should not extract an email address if there isn\'t one' do emails = Extractor.emails("email(at)address(dot)com") emails.must_equal false end it "should extract email addresses" do emails = Extractor.emails("email.address@domain.co.uk") emails.must_include "email.address@domain.co.uk" end end </code></pre> <p>The last test fails because the regular expression doesn't anticipate the majority of valid email addresses. See if you use this as a starting point to come up with or find a better regular expression. To help build your regular expressions, check out <a href="http://rubular.com/r/eHZpt5CXlX" rel="nofollow">Rubular</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload