Get email ids from a given text or email corpora using Regex and Java

Here is the example to get all email ids from a email corpora or text:

package com.Nur;

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

public class Emailvalidation {

    /**
     * @param args
     * @author nur.haldar
     * @return
     */
    public static final String EXAMPLE_TEST = "This is the emailid: nur.haldar@comviva.com" +
            " from where" +
            " the mail was sent to NURJAMIA@gmail.com. Please check " +
            "the two email ids are valid or not";
    public static void main(String[] args) {
        String regex = "\\b[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
        Pattern pattern = Pattern.compile(regex);
       
        Matcher matcher = pattern.matcher(EXAMPLE_TEST);
        System.out.println("Below are the email ids found in the text:");
        while(matcher.find()){
            System.out.println("Email Ids: "+matcher.group());
        }

    }

}

Output: Below are the email ids found in the text:
Email Ids: nur.haldar@comviva.com
Email Ids: NURJAMIA@gmail.com

Comments

Popular posts from this blog

attach file gives false disk address path as 'C:/fakepath/filename'

How to fix World-writable config file /etc/my.cnf is ignored?

Ajmer tour on 18th June '11