Java Email Validate getFirstEmailAddr(String s)

Here you can find the source of getFirstEmailAddr(String s)

Description

Returns the first email address found in the given string or null if not found.

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static String getFirstEmailAddr(String s) 

Method Source Code

//package com.java2s;

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

public class Main {
    private static final String REGEX_EMAIL = "[\\w\\.%\\+-]+@[\\w\\.%\\+-]+\\.[A-Za-z]+";
    private static Pattern patternEmail = null;

    /**/*www  .  j a  va  2s.c  o m*/
     * Returns the first email address found in the given string or <code>null</code> if not found.
     * @param s
     * @return
     */
    public static String getFirstEmailAddr(String s) {
        if (s == null) {
            return null;
        }
        if (patternEmail == null) {
            patternEmail = Pattern.compile(REGEX_EMAIL);
        }
        Matcher matcher = patternEmail.matcher(s);
        if (matcher.find()) {
            return matcher.group();
        } else {
            return null;
        }
    }
}

Related

  1. format(String emails)
  2. getEmail(String author)
  3. getEmailAddressFromDN(String dn)
  4. getEmailListStr(String tempStr)
  5. getEmails(String str)
  6. getSafeMailAddr(String mailAddr)
  7. getSmtpPort(String email)
  8. isEmail(CharSequence email)
  9. isEmail(CharSequence input)