Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.regex.*;
  2.  
  3. public class ValidateEmail {
  4.  
  5.     private static Pattern pattern;
  6.     private static Matcher matcher;
  7.    
  8.     public static void main(String[] args)
  9.     {
  10.         String[] testEmails = { "dean@hotmail.co.uk", "joe.melsha@live.com", "dean_a_g@hotmail.com", "dean..fail@live.com" };
  11.         for (int index = 0; index < testEmails.length; index++)
  12.             System.out.println("Email: " + testEmails[index] + " is " + (validEmail(testEmails[index]) ? "valid." : "invalid."));
  13.     }
  14.  
  15.     public static boolean validEmail(String address)
  16.     {
  17.         return (matcher = pattern.matcher((CharSequence) address)).matches();
  18.     }
  19.  
  20.     static {
  21.  
  22.         String regex = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
  23.         pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  24.    
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement