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 enrich the address to learn who’s actually behind it.

Beyond Regex: Know Who Just Signed Up

A regex only validates format. It can’t tell you that an address is a throwaway, that the domain has no DNS MX records, or that the signup came from an enterprise. Fuego’s lead enrichment API answers those questions in one call, fast enough to run inside your signup flow:

require 'net/http'
require 'json'
require 'uri'

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

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

lead = enrich_email("[email protected]")
puts lead["category"]   # "enterprise"
puts lead["risk"]       # "low"
puts lead["disposable"] # false

The category tells you what kind of organization is behind the address (enterprise, smb, education, government, nonprofit or individual), so you can route enterprise signups to sales and tailor onboarding without a signup survey. The response also includes a did_you_mean typo suggestion and organization data: name, industry, employee range, and country.

Conclusion

A practical regex can be a quick filter for email format validation, but for critical applications, pair it with a lead enrichment API to block disposable signups and understand who your users are. Start a free trial to try it out.

Segment your next signup in under 200ms

Start a free trial today. No survey, no scraped data, no lost signups.