MarcosBL

Aprendiz de todo, maestro de nada

Validando correos con expresiones regulares

Email RegexpCada vez que nos enfrentamos a la programación de un sistema de usuarios, formulario de contacto o similar, inevitablemente encontramos el problema de validación de datos: Si bien es imposible validar un nombre y apellidos, si es común tener que validar el formato de DNI’s o teléfono y, en mi caso, la más temida de todas: el Email

Si ya de por si es pesado controlar todas las diferentes opciones (usuario@dominio.tld, nombre.apellido@dominio.tld, tlds de dos letras, de 3, de 4… o incluso de 6) encima nos encontramos «gracias» como la de las multicuentas de google, con los filtros usuario+filtro@gmail.com y similares.

Por documentarnos, nos vamos a ver el RFC 822, que define y delimita la validez del protocolo de mensajería en internet, y es aqui cuando, si pretendemos que nuestro sistema acepte toda posible combinación válida de emails, la pesadilla no ha hecho más que empezar: Escribir la expresión regular que filtre, limpie y de esplendor a semejante cantidad de normas y posibilidades es trabajo sólo al alcance de unas pocas mentes privilegiadas.

Y sin embargo, YA SE HA HECHO ANTES.

Concretamente, Paul Warren en 2002. Señores, señoras, gentuza varia… les presento la expresión regular para validar correos por excelencia:

[php]
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:
\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(
?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[
\t]))*»(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\0
31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\
](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+
(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:
(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)
?[ \t])*)*\< (?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\
r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[
\t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)
?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]
)*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[
\t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*
)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]
)+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)
*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+
|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r
\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:
\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t
]))*»(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031
]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](
?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?
:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?
:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?
:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?
[ \t]))*»(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\».\[\]
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|
\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>
@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»
(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\
«.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?
:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[
\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\».\[\] \000-
\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(
?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)*\< (?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;
:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([
^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\»
.\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\
]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\
[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\
r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\]
\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]
|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\».\[\] \0
00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\
.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,
;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|»(?
:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*
(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».
\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[
^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\]
]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(
?:(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\
«.\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)(?:\.(?:(
?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[
\[«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t
])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t
])+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?
:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|
\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:
[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».\[\
]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)*\< (?:(?:\r\n) ?[ \t])*(?:@(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[»
()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)
?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>
@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[
\t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,
;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]
)*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\
«.\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?
(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[«()<>@,;:\\».
\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:
\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[
«()<>@,;:\\».\[\]]))|»(?:[^\»\r\\]|\\.|(?:(?:\r\n)?[ \t]))*»(?:(?:\r\n)?[ \t])
*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])
+|\Z|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\
.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\».\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z
|(?=[\[«()<>@,;:\\».\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(
?:\r\n)?[ \t])*))*)?;\s*)

[/php]