Java Email Validate getEmails(String str)

Here you can find the source of getEmails(String str)

Description

get Emails

License

Apache License

Declaration

public static List<String> getEmails(String str) 

Method Source Code


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

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

public class Main {
    public static String CN_SUFFIX = ".cn";
    public static String COM_SUFFIX = ".com";
    private static final Pattern EMALE_PATTERN = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");

    public static List<String> getEmails(String str) {
        Matcher matcher = EMALE_PATTERN.matcher(str);

        List<String> list = null;
        String email = null;/*from   w  ww .  ja  v a  2  s .c o  m*/

        while (matcher.find()) {
            if (list == null) {
                list = new ArrayList<String>();
            }

            email = matcher.group();

            // 1269823266@qq.com<img class
            // 1721913835@qq.com<br
            // 742363596@qq.co
            // tangxj@tiebahl
            if (email.endsWith(CN_SUFFIX) || email.endsWith(COM_SUFFIX)) {
                // right format
            } else {
                int pos = email.lastIndexOf(COM_SUFFIX);
                if (pos > 0) {
                    email = email.substring(0, pos + COM_SUFFIX.length());
                } else {
                    pos = email.lastIndexOf(CN_SUFFIX);
                    if (pos > 0) {
                        email = email.substring(0, pos + CN_SUFFIX.length());
                    } else {
                        continue;
                    }
                }
            }

            list.add(email);

        }

        return list;
    }
}

Related

  1. extractEmailAddresses(final String text)
  2. format(String emails)
  3. getEmail(String author)
  4. getEmailAddressFromDN(String dn)
  5. getEmailListStr(String tempStr)
  6. getFirstEmailAddr(String s)
  7. getSafeMailAddr(String mailAddr)
  8. getSmtpPort(String email)
  9. isEmail(CharSequence email)