expresión regular de correo electrónico muy suelta

I want a very loose regular expression for validating emails

Algunos ejemplos:

anyCharacter@anycharacter.anycharacter

Spaces will be considered invalid (including the ones at the end and the beginning), more than one @, or a dot after the @:

  1. $£"$@$£"$@kdjsad$"£$.dsad343 - IMPORTANTE
  2. ξδησκξδη@φδσαφδσ.φδσφ - valid (all utf-8 chars should be valid)
  3. hdjsh jdhsd.gmail.com - non valid
  4. ldksl .gmail.com - non valid
  5. dldks.gma il.com - non valid
  6. test@.gmail.com - non valid
  7. £££τεστtest@gma!"¬ilγμαιλ.ψψομcomd**%%$ - IMPORTANTE

I'm trying to modify this one ^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ but i have some troubles and your help would be appreciated.

preguntado el 02 de febrero de 12 a las 10:02

i came up to this ^\D+([-+.']\D+)*\S[^\@]+@[^\.]+[^\.]\D\S+([-.]\D+)*\.\D\S+([-.]\D+)*$ but still needs work -

1 Respuestas

You should be very careful when validating email addresses. I'm not saying you shouldn't do it, but you need to be aware that to write a 100% accurate email address validation is going to be extremely difficult and by having a not-quite perfect one, you may still be allowing invalid addresses and (worse) prevent legitimate users.

There are lots of obscure cases that are technically valid (even though they are rarely used and sooner or later are likely to break a badly written email server somewhere in the world). You need to decide whether you want to allow addresses from that minority of users.

You might have a user who has (been daft enough to get) an email address containing a quoted @ sign. e.g. "the-address-has-two-@-symbols"@example.com

Infact, you can have almost any character you can think of in the non-domain part of an address (as long as they're quoted), even spaces can appear: "Forename Surname"@example.com

En tu ejemplo: £££τεστtest@gma!"¬ilγμαιλ.ψψομcomd**%%$ would actually be invalid, because domain names may only contain letters (a-z), numbers, dots and hyphens. So assuming you are doing a case-insensitive match and you do want to check for valid domain names, you should be able to simplify the expression (taken from your comment) to

^\D+([-+.']\D+)*\S[^\@]+@[a-z0-9]+[a-z0-9\-\.]*$

You can also take the domain validation further, but to do it correctly will require reading RFC 2396.

Respondido 28 ago 13, 16:08

thanks for you answer. But since you can buy domains names e.g τεστ.com, boom123@τεστ.com, shouldn't be a valid email address? How can I modify the regex to support this? - boom123

I came up to this ^\D+([-+.']\D+)*\S[^\@]+@[^\.]\S\D\S+[a-z0-9\-\.]*$ - boom123

To handle internationalised domain names, you will need to convert them to Punycode first, or revert to a more basic validation. τεστ.com doesn't actually exist, it's just a representation of the real domain (which will be alphanumerics and hyphens). See en.wikipedia.org/wiki/Punycode . When it comes to internal processing, it is probably more reliable to work with the ASCII representation of international domains. So you may want to convert the domain names, then validate. - SimonMayer

im not good at all with regex...as my friend above said the requirements that i want my regex to satisfy are. 1. if the first part of the email is in quotes e.g "the-message-has-two-@-symbols"@example.com match anycharacter "anycharacter"@email.com if not i want proper validation. the main requirement is to be able to validate unicode character in the username part and the domain part...if someone can help me a bit..i will try to finish it...not asking for a ready solution...thanks... - boom123

I don't know browsers/DNS handle non-english alphabet but it looks like China is allowed to use, per ICANN, .中国 ( en.wikipedia.org/wiki/… ) so technically nateisawesome@nate.中国 should be a legal email (as far as I understand email/DNS). - Natalie Adams

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.