Here you can find the source of getEmails(String str)
public static List<String> getEmails(String str)
//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; } }