Advertisement
Guest User

regex email

a guest
Feb 8th, 2017
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 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}(\\.[A-Z]{2,4})?");
  19.   std::regex_match(mail, pattern);
  20.  }
  21.  
  22.  email* print()
  23.  {
  24.    std::cout<<"\n\tEmail: "<<mail<<(email(mail).isMail()?" Valid":" Invalid")<<"\n";
  25.  }
  26. };
  27.  
  28. int main(void)
  29. {
  30.   std::string email1 = "regex_cpp@net.br";
  31.   std::string email2 = "regex_cpp@terra.com.br";
  32.  
  33.   std::string email3 = "regex_cpp@hotmail.com.br.net"; /* nao pode validar */
  34.   std::string email4 = "regex_cpp@hotmail.com";
  35.  
  36.   std::string email5 = "regex_cpp@yahoo.com.br";
  37.   std::string email6 = "regex_cpp@gmail.com";
  38.  
  39.   std::string email7 = "regex_cpp@bol.com.br";
  40.   std::string email8 = "regex.cpp@bol.com";
  41.  
  42.   std::string email9 = "reg_ex.cpp@bol.com";
  43.   std::string email10 = "reg_ex.cpp@org.br";
  44.   std::string email11 = "reg_ex.cpp@net.org.br";
  45.  
  46.   email(email1).print();
  47.   email(email2).print();
  48.   email(email3).print();
  49.   email(email4).print();
  50.   email(email5).print();
  51.   email(email6).print();
  52.   email(email7).print();
  53.   email(email8).print();
  54.   email(email9).print();
  55.   email(email10).print();
  56.   email(email11).print();
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement