Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.*;
- public class ValidateEmail {
- private static Pattern pattern;
- private static Matcher matcher;
- public static void main(String[] args)
- {
- String[] testEmails = { "dean@hotmail.co.uk", "joe.melsha@live.com", "dean_a_g@hotmail.com", "dean..fail@live.com" };
- for (int index = 0; index < testEmails.length; index++)
- System.out.println("Email: " + testEmails[index] + " is " + (validEmail(testEmails[index]) ? "valid." : "invalid."));
- }
- public static boolean validEmail(String address)
- {
- return (matcher = pattern.matcher((CharSequence) address)).matches();
- }
- static {
- String regex = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
- pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement