validar email o dirección de correo con Pattern y Matcher

Iniciado por kinos, 3 Agosto 2009, 19:46 PM

0 Miembros y 2 Visitantes están viendo este tema.

kinos

#10
Hola Danther, he estao mirando el codigo y en validateEmail() yo quitaría lo de "validate country code" porque creo que se podría hacer directamente con la expresión regular.

En este sitio he visto también una expresión regular pero creo que es .net:

http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

 string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";


Código (java) [Seleccionar]

package expresionesregularesemail;
import java.util.regex.*;
import java.util.StringTokenizer;

public class Main {
public boolean validateEmail(String email){
   //Pattern p = Pattern.compile("[a-zA-Z0-9]+[.[a-zA-Z0-9_-]+]*@[a-zA-Z0-9_]+[.[a-zA-Z]+]+"); //original
   Pattern p = Pattern.compile("[a-zA-Z0-9]+[.[a-zA-Z0-9_-]+]*@[a-z0-9][\\w\\.-]*[a-z0-9]\\.[a-z][a-z\\.]*[a-z]$");

 
  //@"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$"
 
  // Match the given string with the pattern
  Matcher m = p.matcher(email);
  return m.matches();
}
   public static void main(String[] args) {
       Main miMain = new Main();
       System.out.println("*****************************validateEmail******************************************");
       System.out.println("email: "+"example@cs.uns.edu.ar "+miMain.validateEmail("example@cs.uns.edu.ar"));
       System.out.println("email: "+"name-surname@computer_name.net_name.com "+miMain.validateEmail("name-surname@computer_name.net_name.com"));
       System.out.println("email: "+"!u@hotm@ail.co "+miMain.validateEmail("!u@hotm@ail.co"));
       System.out.println("email: "+"u@hotm@ail.co "+miMain.validateEmail("u@hotm@ail.co"));
       System.out.println("email: "+"u@hotmail.co "+miMain.validateEmail("u@hotmail.co"));
       System.out.println("email: "+"u@hotmail.c "+miMain.validateEmail("u@hotmail.c"));
       System.out.println("email: "+"!u@hotmail.cc "+miMain.validateEmail("!u@hotmail.cc"));
       System.out.println("email: "+"uu@hotmail.cc "+miMain.validateEmail("uu@hotmail.cc"));
       System.out.println("email: "+"@hotmail.cc "+miMain.validateEmail("@hotmail.cc"));
       System.out.println("email: "+".u@hotmail.cc "+miMain.validateEmail(".u@hotmail.cc"));
       System.out.println("email: "+"u.u@hotmail.cc "+miMain.validateEmail("u.u@hotmail.cc"));
       System.out.println("email: "+"u u@hotmail.cc "+miMain.validateEmail("u u@hotmail.cc"));
       System.out.println("email: "+"u@h.cc "+miMain.validateEmail("5u@h.cc"));
       System.out.println("email: "+"5u@h.cc "+miMain.validateEmail("5u@h.cc"));
       System.out.println("email: "+"5.cc "+miMain.validateEmail("5.cc"));
       System.out.println("email: "+"5@h.cc "+miMain.validateEmail("5@h.cc"));
       System.out.println("email: "+"5@h.cc.d "+miMain.validateEmail("5@h.cc.d"));
       System.out.println("email: "+"5@h.cc.dd "+miMain.validateEmail("5@h.cc.dd"));
       System.out.println("email: "+"5@h.c.dd "+miMain.validateEmail("5@h.c.dd"));
       System.out.println("email: "+"avf-@h.c.dd "+miMain.validateEmail("avf-@h.c.dd"));
       System.out.println("email: "+"a-vf@h.c.dd "+miMain.validateEmail("a-vf@h.c.dd"));
       System.out.println("email: "+"avf@h.c.dd "+miMain.validateEmail("avf@h.c.dd"));
       System.out.println("email: "+"-avf@h.c.dd "+miMain.validateEmail("-avf@h.c.dd"));
       System.out.println("email: "+"_avf@h.c.dd "+miMain.validateEmail("_avf@h.c.dd"));
       System.out.println("email: "+"a-_-_.-_b--d__-@h.c.dd "+miMain.validateEmail("a-_-_.-_b--d__-@h.c.dd"));
       System.out.println("email: "+"a_._-@h.c.dd "+miMain.validateEmail("a_._-@h.c.dd"));
       System.out.println("email: "+"a_.-@h.c.dd "+miMain.validateEmail("a_.-@h.c.dd"));
       System.out.println("email: "+"a.-@h.c.dd "+miMain.validateEmail("a.-@h.c.dd"));

   }
}