lundi 21 mai 2018

C++: Regular expression for email

The answers to this similar question are insufficient. So I'm making the question more accurate.

The regular expression for an email I found here. This is it:

\A(?=[a-z0-9@.!#$%&'*+\/=?^_`\{|\}~-]{6,254}\z)(?=[a-z0-9.!#$%&'*+\/=?^_`\{|\}~-]{1,64}@)[a-z0-9!#$%&'*+\/=?^_`\{|\}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`\{|\}~-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z

I'm trying to implement this in C++, but there's a problem with the ECMAScript syntax apparently, because this works on Regex101:

const std::string _EmailRegexStr =
    R"(\A(?=[a-z0-9@.!#$%&'\*\+\/=?^_`\{|\}~\-]{6,254}\z)(?=[a-z0-9.!#$%&'\*\+\/=?^_`\{|\}~\-]{1,64}@)[a-z0-9!#$%&'\*\+\/=?^_`\{|\}~\-]+(?:\.[a-z0-9!#$%&'\*\+\/=?^_`\{|\}~\-]+)*@(?:(?=[a-z0-9-]{1,63}\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?=[a-z0-9-]{1,63}\z)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\z)";
const std::regex _EmailRegex(_EmailRegexStr);
#include <iostream>
bool IsEmailAddressValid(const std::string& address)
{
    return std::regex_match(address, _EmailRegex);
}

But this doesn't work. Here's a minimal, verifiable and complete example that demonstrates the problem.

PS: I wanna emphasize that simple solutions like ([\w.]+)@([\w]+).(\w{2,}) are not acceptable because they don't handle special cases, like ab..cd@xyz.com.

Aucun commentaire:

Enregistrer un commentaire