Java Email Validate parseEmail(String target)

Here you can find the source of parseEmail(String target)

Description

Parse the target string, and check is obey the email format or not.

License

Apache License

Declaration

public static String parseEmail(String target) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//from w  w  w. j  av  a2 s.  c  o m
     * Parse the target string, and check is obey the email format or not. If not, return null.
     */
    public static String parseEmail(String target) {
        target = parseString(target);
        if (target == null) {
            return null;
        }

        String regexp = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(target);

        return matcher.find() ? target : null;
    }

    /**
     * Pre-process the String object. If object is null, return null; otherwise
     * remove heading and tailing white space.
     */
    public static String parseString(String target) {
        if (target == null) {
            return null;
        } else {
            return target.trim();
        }
    }
}

Related

  1. isValidEmailAddress(String emailAddress)
  2. isValidEmailAddress(String emailAddress)
  3. isValidEmailAddress(String emailAddress)
  4. isValidEmailAddress(String emailAddress)
  5. isValidEmailAdress(String email)
  6. validateEmail(String email)
  7. validateEmail(String email)
  8. validateEmail(String email)
  9. validateEmail(String email)