<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users333</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/333" rel="self"/>
  <title>thesuntoucher.myopenid.com</title>
  <updated>Tue Nov 06 22:20:26 +0000 2007</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor776</id>
    <published>2007-11-06T22:20:26+00:00</published>
    <title>[Java] On Email addresses validator</title>
    <content type="html">&lt;p&gt;Okay, guys, this is my version. I would like to point some things out. The Regex i use, i found years ago somewhere on the internet and use since than, checks a little more: TLDs can have between two and seven chars and so on.&lt;/p&gt;

&lt;p&gt;I tried to make this more generic an implemented a method that gets a reader and a writer, so you could give it a FileReader or any other Reader.&lt;/p&gt;

&lt;p&gt;Last but not least, i don't read this in a List and don't sort it so you could throw gigabytes of data in it without running out of memory.&lt;/p&gt;

&lt;p&gt;hope that helps
&lt;br /&gt;Tim&lt;/p&gt;

&lt;pre&gt;## Java [java]

import java.io.*;
import java.util.regex.Pattern;
	
public class RefactorMyCodeEmail {
	
	private static Pattern validEmail = Pattern.compile(&amp;quot;[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}&amp;quot;);

    public static void main(String[] args) throws IOException {
    	
    	FileReader in = null; 
    	FileWriter out = null;
    	
    	try {
    	
	    	in = new FileReader(new File(args[0]));
	    	out = new FileWriter(new File(args[1]));
	    	
	        filterEmailsFile(in, out);
        
		}finally{
			if(in != null) in.close();
			if(out != null) out.close();
		}
    }
    
    private static void filterEmailsFile(Reader in, Writer out) throws IOException {
        
        BufferedReader reader = new BufferedReader(in);
        BufferedWriter writer = new BufferedWriter(out);
        
        String email = null;
        while ((email = reader.readLine()) != null) {
            if(validEmail.matcher(email).matches()){
	        	writer.write(email);
	        	writer.newLine();
            }
        }
    }
}
&lt;/pre&gt;</content>
    <author>
      <name>thesuntoucher.myopenid.com</name>
      <email>timbuethe@gmx.de</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/73-email-addresses-validator/refactors/776" rel="alternate"/>
  </entry>
</feed>
