Blog / Tutorial

Email Regex Ruby: Validate Email Addresses with Ease

Matthew Rathbone /

Email validation is a crucial part of ensuring the legitimacy of user data in a variety of applications. While using regex to validate email addresses isn’t perfect, it can be a useful tool for catching some common mistakes before using more advanced methods. In this tutorial, we’ll go through the basics of using regex in Ruby to validate email addresses.

The Simplified Regex Pattern

First, let’s look at a regex pattern that does a “good enough” job for most use cases. The goal is to stay practical without getting overly complex.

email_regex = /\A[^@\s]+@[^@\s]+\z/

Explanation

  • \A and \z: Ensure the match is against the entire string.
  • [^@\s]+: Ensures there are no spaces or @ symbols in the local part of the email address.
  • @: The at symbol separating the local part from the domain part.
  • [^@\s]+: Ensures there are no spaces or @ symbols in the domain part.

Ruby Code Example

Here’s a small Ruby script that uses this pattern to validate email addresses:

def valid_email?(email)
  email_regex = /\A[^@\s]+@[^@\s]+\z/
  email.match?(email_regex)
end

# Test cases
puts valid_email?("[email protected]")  # Expected output: true
puts valid_email?("[email protected]")  # Expected output: true
puts valid_email?("plainaddress")  # Expected output: false
puts valid_email?("@missinglocal.org")  # Expected output: false
puts valid_email?("[email protected]")  # Expected output: true

Example Results

  • Valid emails:
  • Invalid emails:
    • plainaddress : false
    • @missinglocal.org : false
  • Slips through:
    • [email protected] : true. The domain can’t really start with a dot, but this pattern only checks for an @ with no spaces around it. That’s the trade-off of a simple pattern, and since no real domain is behind addresses like this, a DNS lookup on the domain will catch them.

Why Not Overcomplicate?

You’ve probably seen extremely convoluted regex patterns for email validation. However, being too strict is often not worth the effort. Email providers frequently accept a wide range of email formats; hence, if your regex becomes too stringent, you might end up rejecting valid addresses.

Instead of relying solely on regex, it’s far more effective to catch the most common format issues with a simple regex and then verify the address with an API.

Beyond Regex: Verify the Address

A regex only checks format. It can’t tell you whether the mailbox actually exists, whether the domain accepts every address you throw at it, or whether the address is a disposable throwaway. Fuego’s email verification API answers those questions with a real SMTP check, and includes domain intelligence about the company behind the address at no extra cost:

  • Verify: a result of deliverable, undeliverable, risky or unknown, plus a reason like accepted_email or invalid_domain
  • Flag risky signups: disposable, role, free and accept_all flags
  • Fix typos: a did_you_mean suggestion for mistakes like gmial.com
  • Know the company: a free domain_insight object with the organization’s name, industry, employee count and country
require 'net/http'
require 'json'
require 'uri'

def verify_email(email)
  uri = URI("https://app.gofuego.io/api/v1/verify")
  uri.query = URI.encode_www_form(token: ENV["FUEGO_KEY"], email: email)

  JSON.parse(Net::HTTP.get(uri))
end

check = verify_email("[email protected]")
puts check["result"]                  # "deliverable"
puts check["disposable"]              # false
puts check["domain_insight"]["name"]  # "Jupiter Mining Corporation"

For a full walkthrough, see our guide to integrating the Fuego API with Ruby.

Conclusion

A practical regex can be a quick filter for email format validation, but for critical applications, pair it with an email verification API to make sure the address is real and see who’s behind it. Start a free trial to try it out.

Ready to verify smarter?

Email verification with free domain intelligence on every check. 100 free credits to start.