Java Code To Extract Email From Text

I found a good piece of code which can be used to extract multiple email from a String. A modified version of the code is listed below:

public static String readContactEmailFromString(String resumeText) {
    final String RE_MAIL = "([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})";
    Pattern p = Pattern.compile(RE_MAIL);
    Matcher m = p.matcher(resumeText);

    StringBuilder sb = new StringBuilder();
    while(m.find()) {
        if (sb.toString().contains(m.group(1))) continue;
        if (sb.toString().isEmpty()) {
            sb.append(m.group(1));
        } else {
            sb.append("; " + m.group(1));
        }
    }
    return sb.toString();
}

Leave a Reply

Your email address will not be published. Required fields are marked *