Chcę napisać funkcję, która sprawdza poprawność adresu email, znalazłem przykład w Internecie oparty o regex:
#include <regex>
#include <iostream>
#include <string>
bool is_email_valid(const std::string& email)
{
// define a regular expression
const std::regex pattern
("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
// try to match the string with the regular expression
return std::regex_match(email, pattern);
}
int main()
{
std::string email1 = "[email protected]";
std::string email2 = "[email protected]";
std::string email3 = "[email protected]";
std::string email4 = "marius@domain";
std::cout << email1 << " : " << (is_email_valid(email1) ?
"valid" : "invalid") << std::endl;
std::cout << email2 << " : " << (is_email_valid(email2) ?
"valid" : "invalid") << std::endl;
std::cout << email3 << " : " << (is_email_valid(email3) ?
"valid" : "invalid") << std::endl;
std::cout << email4 << " : " << (is_email_valid(email4) ?
"valid" : "invalid") << std::endl;
return 0;
}
Niestety po kompilacji dostaje wyjątek: "regex_error", dlaczego to nie działa? Ewentualnie prosiłbym o inne metody sprawdzenia poprawności adresu e-mail, oczywiście mógłbym napisać to "na pałę", ale chciałbym tego uniknąć.