Skip to main content

Java code for extracting email addresses from a text file using regular expression

import java.io.*;
import java.util.regex.*;

public class harness {
 /**
  * @param args
  */
 public static void main(String[] args) {
 // TODO Auto-generated method stub
        File aFile = new File(<bulk_content.txt>);
        Pattern regexp = Pattern.compile("");
        Matcher matcher = regexp.matcher("([a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+)");
        LineNumberReader lineReader = null;
        try {
         FileWriter fstream = new FileWriter("<emails.txt>");
         BufferedWriter out = new BufferedWriter(fstream);
         lineReader = new LineNumberReader(new FileReader(aFile));
               String line = null;
               while ((line = lineReader.readLine()) != null){
               matcher.reset(line);
               while (matcher.find()) {
                 out.write(matcher.group() + "\n");
               }
              }
        }
        catch (FileNotFoundException ex) {
         ex.printStackTrace();
        }
        catch (IOException ex){
         ex.printStackTrace();
        }
        finally {
         try {
          if (lineReader!= null) lineReader.close();
            }
            catch (IOException ex) {
             ex.printStackTrace();
            }
        }      
 }
}

Comments

Popular posts from this blog