Java Email Validate extractEmailAddresses(final String text)

Here you can find the source of extractEmailAddresses(final String text)

Description

This method analyses a text for email addresses and returns a set of those found.

License

Open Source License

Parameter

Parameter Description
text the text

Return

a set of email addresses

Declaration

public static Set<String> extractEmailAddresses(final String text) 

Method Source Code


//package com.java2s;
/*//from w  w  w  . ja v a2s  . c  o  m
 * Copyright (c) 2010 Stiftung Deutsches Elektronen-Synchrotron,
 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY.
 *
 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS.
 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE
 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR
 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE.
 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
 * OR MODIFICATIONS.
 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION,
 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS
 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY
 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM
 */

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

public class Main {
    public static final String EMAIL_REG_EXP = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

    /**
     * This method analyses a text for email addresses and returns a set of those found.
     * The regular expression is from the official standard: RFC 2822 and can be found here:
     * http://www.regular-expressions.info/email.html
     *
     * Another source for valid email address format can be found here:
     * http://groups.google.com/group/eaut/web/rules-for-valid-email-addresses?pli=1
     *
     * @param text the text
     * @return a set of email addresses
     */
    public static Set<String> extractEmailAddresses(final String text) {
        final Set<String> addresses = new HashSet<String>();

        final Pattern p = Pattern.compile(EMAIL_REG_EXP);
        final Matcher m = p.matcher(text);

        while (m.find()) {
            addresses.add(m.group(0));
        }
        return addresses;
    }
}

Related

  1. checkEmailPattern(String email)
  2. checkEmailWithSuffix(String email)
  3. cleanUpEmailAddress(CharSequence address)
  4. containsEmail(String chunk)
  5. extractEmail(String string, StringBuffer sb)
  6. format(String emails)
  7. getEmail(String author)
  8. getEmailAddressFromDN(String dn)
  9. getEmailListStr(String tempStr)