Check if a string is email Format via regex - Java java.util.regex

Java examples for java.util.regex:Match Email

Description

Check if a string is email Format via regex

Demo Code

import java.util.regex.Pattern;

public class Main{

    public static void main(String[] argv){
        String email = "java2s.com";
        System.out.println(emailFormat(email));
        // w  w  w  . j  a  v a2s .  c om
        email = "asdf@java2s.com";
        System.out.println(emailFormat(email));
    }

    public static boolean emailFormat(String email) {
        String pattern = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        return Pattern.compile(pattern).matcher(email).find();
    }

}

Related Tutorials