Validação de emails em C/C++
Publicado por César (última atualização em 28/02/2019)
[ Hits: 3.680 ]
Estudando regex para validação de datas fiquei interessado em saber como validar emails usando a mesma, então elaborei um script em C++ usando orientação a objetos, e outro em c.
Para compilar o script email.cxx basta fazer:
$ g++ -oemail email.cxx -std=c++11
Para compilar o script email.c basta fazer:
$ gcc -oemail email.c
Espero que o mesmo seja útil.
#include <regex>
#include <iostream>
/*!
*
* @begin @section terms_of_use Terms of Use
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0+ of the License, or (at your option) any later version.
*
* @end @section terms_of_use Terms of Use
*
* @begin @section author Author
*
* @file email.cxx
* @version 0.1
* @brief validação de emails em c++ usando regex.
* @author Jean Zonta
* @pseudonym wiki.anon
* @Copyright/modification (C) 2018 Jean Zonta.
*
* @end @section author Author
*
*/
struct email
{
email(const std::string& mail): _email(mail){}
virtual ~email(){ if(!_email.empty())_email.clear(); }
bool isEmail()
{
//valida todos os dominios e servidores de emails
//const std::regex pattern("^([a-zA-Z0-9._]+@[a-z]+([.][a-z]{2,4}){1,2})$");
//const std::regex pattern("^(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))?(\\.(\\w+))$");
//valida somente servidores @{hotmail,terra,yahoo,uol,bol} no dominio .com.br e servidor gmail no dominio .com
const std::regex pattern("^([a-zA-Z0-9._]+@((hotmail|terra|yahoo|uol|bol)[.](com[.]br))?(gmail[.]com)?)$");
return std::regex_match(_email, pattern);
}
void print()
{
std::cout << "\n\tEmail: "+_email+(isEmail()?" is Valid\n":" is Invalid\n");
}
private:
std::string _email;
};
int main()
{
std::string emails[16] = {
"regex_cpp@net.br","reg_ex.cpp@org.nz",
"reg_ex.cpp@net.org.br","regex_cpp@terra.com.br.net",
"regex_cpp@hotmail.com","regex_cpp@hotmail.com.br",
"regex_cpp@yahoo.com","regex_cpp@yahoo.com.br",
"regex_cpp@gmail.com","reg_ex.cpp@gmail.com.br",
"regex_cpp@uol.com","regex_cpp@uol.com.br",
"regex.cpp@bol.com","regex_cpp@bol.com.br",
"regex_email@terra.com","reGe.x_email@terra.com.br"
};
for(int i = 0; i < 16; i++)
{
email(emails[i]).print();
}
/*
char input[30]={0};
do{
std::cout << "\n\tDigite um email: ";
std::cin >> input;
email(input).print();
}while(!email(input).isEmail());
*/
std::cout << "\n";
return 0;
}
Formatar strings em C (RG, telefone, CEP etc)
Nenhum comentário foi encontrado.
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
Necessário autenticar ao imprimir - Ubuntu X Windowns (1)
O que houve com slackware ??? (11)
O Free Download Manager não abre no Fedora 43 KDE Plasma (2)









