is a string representing an Email - Java java.util.regex

Java examples for java.util.regex:Match Email

Description

is a string representing an Email

Demo Code

import java.util.regex.Pattern;

public class Main {

  public static void main(String[] argv) {
    String email = "java2s.com";
    System.out.println(isEmail(email));
    //from  ww  w  .  j  a va2s.com
    
    email = "asdf@java2s.com";
    System.out.println(isEmail(email));
  }

  private final static Pattern EMAIL = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

  public static boolean isEmail(String email) {
    return !isEmpty(email) && EMAIL.matcher(email).matches();
  }

  public static boolean isEmpty(String str) {
    return str == null || str.trim().length() == 0 || str.trim().equalsIgnoreCase("null");
  }

}

Related Tutorials