Advertisement
Guest User

regex para email

a guest
Feb 8th, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <exception>
  2. #include <iostream>
  3. #include <regex>
  4.  
  5. class email
  6. {
  7.  private:
  8.   std::string mail;
  9.  public:
  10.    
  11.    email(std::string mail)
  12.    {
  13.     this->mail = mail;
  14.    }
  15.  
  16.  bool isMail()
  17.  {
  18.   //const std::regex pattern("[A-Z0-9._%-]+@[A-Z0-9.-]+?\\.[A-Z]{2,4}b\\.[A-Z]{2,4}");
  19.    const std::regex pattern("[A-Z0-9._%-]+@[A-Z0-9.-]+?\\.[A-Z]{2,4}?(\\.[A-Z]{2,4})?");
  20.   std::regex_match(mail, pattern);
  21.  }
  22.  
  23.  email* print()
  24.  {
  25.    std::cout<<"\n\tEmail: "<<mail<<(email(mail).isMail()?" Valid":" Invalid")<<"\n";
  26.  }
  27. };
  28.  
  29. int main(void)
  30. {
  31.   std::string email1 = "regex_cpp@net.br";
  32.   std::string email2 = "regex_cpp@terra.com.br";
  33.  
  34.   std::string email3 = "regex_cpp@hotmail.com.br.net"; /* nao pode validar */
  35.   std::string email4 = "regex_cpp@hotmail.com";
  36.  
  37.   std::string email5 = "regex_cpp@yahoo.com.br";
  38.   std::string email6 = "regex_cpp@gmail.com";
  39.  
  40.   std::string email7 = "regex_cpp@bol.com.br";
  41.   std::string email8 = "regex.cpp@bol.com";
  42.  
  43.   std::string email9 = "reg_ex.cpp@bol.com";
  44.   std::string email10 = "reg_ex.cpp@org.br";
  45.   std::string email11 = "reg_ex.cpp@net.org.br";
  46.  
  47.   email(email1).print();
  48.   email(email2).print();
  49.   email(email3).print();
  50.   email(email4).print();
  51.   email(email5).print();
  52.   email(email6).print();
  53.   email(email7).print();
  54.   email(email8).print();
  55.   email(email9).print();
  56.   email(email10).print();
  57.   email(email11).print();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement