Validate email with regular expression - Java java.util.regex

Java examples for java.util.regex:Match Email

Description

Validate email with regular expression

Demo Code


//package com.java2s;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] argv) {
        String email = "java2s.com";
        System.out.println(isEmailValid(email));
    }//from w  w w.j a v  a2  s.  c  om

    private static Pattern pattern;
    private static final String EMAIL_PATTERN = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$";


    public static boolean isEmailValid(final String email) {
        boolean isValid = false;
        if (email != null) {
            if (pattern == null) {
                pattern = Pattern.compile(EMAIL_PATTERN);
            }
            isValid = pattern.matcher(email).matches();
        }
        return isValid;
    }
}

Related Tutorials