Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Please evaluate: RegEx for validating e-mail addresses

by jdalbec (Deacon)
on Sep 26, 2004 at 17:17 UTC ( [id://393980]=note: print w/replies, xml ) Need Help??


in reply to Please evaluate: RegEx for validating e-mail addresses

  1. ([a-z]|[0-9])+ : One or more letters and/or numbers
  2. ((\.)+([a-z]|[0-9])+)* : Possibly a dot. Considering that there is a dot one or more letters and/or numbers must follow
  3. (\-|\_)* : Possibly a dash or underscore.
  4. ([a-z]|[0-9])* : Possibly more letters and/or numbers
  5. \@{1} : Must have one (and only one) '@'
  6. ([a-z]|[0-9])+ : Must have more letters and /or numbers followed by
  7. \.{1} : a dot that must be followed by
  8. ([a-z]|[0-9]){2,} : at least two letters and/or numbers
  9. (\.([a-z|0-9]){2})* : and possibly followed by a dot and more letters and/or numbers

Please use <code> tags around Perl code so it displays correctly. You may want to use (?: ... ) for grouping so you don't get a bunch of assignments to $1, $2, etc. Also {1} is redundant. In several cases your comments don't match what the regexp is doing.

In 2. the (\.)+ allows two or more dots in a row. I think you probably just want to drop the + (and the redundant parentheses).

In 3. you have almost the same problem. Try changing * to ? unless you mean to allow multiple dashes/underscores in a row.

In 9. I think you want (?:\.(?:[a-z0-9]){2,})*. I'm leaving the * (even though you said "possibly") since I think you can have any number of domain levels in a FQDN.

janitored by ybiC: Balanced <code> tags around regex examples for proper rendering

Replies are listed 'Best First'.
Re^2: Please evaluate: RegEx for validating e-mail addresses
by DaWolf (Curate) on Sep 27, 2004 at 11:44 UTC
    Hi, jdalbec. Thanks for replying.

    Please use <code> tags around Perl code so it displays correctly.

    Actually I used, but since the <code> tags are inside a <ol> tag maybe they aren't displaying correctly...

    You may want to use (?: ... ) for grouping so you don't get a bunch of assignments to $1, $2, etc.

    True, thanks for pointing this out.

    Also {1} is redundant.

    Also true. Thanks again, but...

    In several cases your comments don't match what the regexp is doing.

    Actually I've made a lot of testing and apparently it is... Could you clarify this part a little? I'm curious.

    In 2. the (\.)+ allows two or more dots in a row. I think you probably just want to drop the + (and the redundant parentheses).

    and

    In 3. you have almost the same problem. Try changing * to ? unless you mean to allow multiple dashes/underscores in a row.

    Actually I want to allow multiple dots, dashes and underscores since 'er.galvao.abbott@somedomain.com' or 'er_galvao_abbott@somedomain.com' are valid addresses.

    In 9. I think you want (?:\.(?:[a-z0-9]){2,})*. I'm leaving the * (even though you said "possibly") since I think you can have any number of domain levels in a FQDN.

    Thanks, I'll give this chunk a try and see what happens.

    Thanks a lot for your reply,

      merlyn says please stop encouraging DaWolf - he's heading down a dangerous path
      It's true that you're heading down a dangerous path. Whatever regex you finally come up with, please don't use it in a production application. Use Email::Valid on the server and don't try to do complex processing in JavaScript.

      What is the application you want to validate e-mail addresses for? Do you only care that they're syntactically valid or do you want to verify that there's a real person on the other end? In the second case you'll need to do more than just run the address through Email::Valid.

      For educational purposes only, I'll point out that your regex rejects 'er_galvao_abbott@somedomain.com' and accepts 'er...galvao...abbott-_-_-_@somedomain.com'. Is that what you want?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://393980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found