Validate email address using Regex in C++

=> home

Qucik one. Something I want to write down before I forget. Validating email addresses have been a constant pain for software developers. The RFC spec for a valid email is complex. No, it's not simply ^\S+@\S+\.\S+$. For example. The spec prohibits email addresses on TLD. Thus bob@example is not valid. There's a very helpful post on the internet that shares how to validate on using regex.

=> Email Address Regular Expression That 99.99% Works.

There's some discussion to have as there's edge cases that are not covered. But it's good enough. One problem. The site does not share how to do this in C++. That's what this post is about.

C++'s standard regex library by default runs in ECMAScript mode. But with different syntax of creating regex. Some quick adoption yields the following code

static const std::regex email_regex(R"(^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$)");
std::smatch match;
if(!std::regex_match(email, match, email_regex)) {
	// invalid email
}

You have it! Happy coding!

=> ========

Okay, C++'s regex library is known to be slow. Replace it with Boost's and done.

Proxy Information
Original URL
gemini://clehaxze.tw/gemlog/2023/01-28-validate-email-address-using-regex-in-cpp.gmi
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
1421.144342 milliseconds
Gemini-to-HTML Time
0.294451 milliseconds

This content has been proxied by September (ba2dc).