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 stringThis 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:
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.
std::string
ClassA detailed guide to std::string
, covering the most essential methods and operators