A Deeper Look at the std::string Class

Validating Email Addresses with Regex

How can I check if a std::string is a valid email address using regular expressions?

Abstract art representing computer programming

Validating email addresses using regular expressions can be tricky due to the complexity of the email address specification. However, we can create a simplified regex that catches most common email formats. Here's an example of how to validate email addresses using C++'s <regex> library:

#include <iostream>
#include <string>
#include <regex>

// This regex pattern covers most
// common email formats
const std::regex pattern(
  R"(^[a-zA-Z0-9._%+-]+@)"
  R"([a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$)"
);

bool is_valid_email(const std::string& email) {
  return std::regex_match(email, pattern);
}

int main() {
  std::vector<std::string> emails{
    "user@example.com",
    "user.name+tag@example.co.uk",
    "invalid.email@",
    "@invalid.com",
    "user@invalid",
    "user@.com"
  };

  for (const auto& email : emails) {
    std::cout << email << " is "
      << (is_valid_email(email)
        ? "valid" : "invalid") << '\n';
  }
}
user@example.com is valid
user.name+tag@example.co.uk is valid
invalid.email@ is invalid
@invalid.com is invalid
user@invalid is invalid
user@.com is invalid

Let's break down the regex pattern:

  • ^ : Start of the string
  • [a-zA-Z0-9._%+-]+ : One or more letters, digits, or certain special characters
  • @ : The @ symbol
  • [a-zA-Z0-9.-]+ : One or more letters, digits, dots, or hyphens
  • \. : A literal dot
  • [a-zA-Z]{2,} : Two or more letters (for the top-level domain)
  • $ : End of the string

This regex covers most common email formats, but it's important to note that it's not a complete implementation of the email address specification (RFC 5322). For example, it doesn't allow for quoted local parts or IP addresses in the domain part.

For a more comprehensive validation, you might want to consider:

  1. Checking the maximum length of the email (RFC 5321 limits it to 254 characters).
  2. Validating the domain using DNS lookups.
  3. Using a more complex regex that covers more edge cases.

Remember, while regex can catch many invalid email addresses, the only way to truly validate an email address is to send an email to it and confirm receipt. For most applications, a simple regex check is sufficient to catch typographical errors and obviously invalid addresses.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved