Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by DaWolf (Curate)
on Sep 25, 2004 at 20:23 UTC ( [id://393841]=note: print w/replies, xml ) Need Help??


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

Please guys, let me make some statements here, since I was apparently misunderstood:

I've understood Ovid's post, davido and really appreciated his contribution (as usual), but I'd like to *learn* to build a regex for that matter.

Just because a "standard" way of building it exists it's not unmutable, e.g.:

Ovid first allows a whitespace and/or tab to be in the beginning of the e-mail address:
[\040\t]*
I wouldn't do that. *My* line of thinking and the logic that reigns on the app I'm coding is: if the user has entered a whitespace in the beginning of the webform field, well, too bad for him. Even better, I'd write a JS function to disable whitespaces on that field. Since it's a web app it's better for me (and most probably for the user) to do it this way.

The *only thing* I want to do is to validate an e-mail address (not something like "My name <myemail@myserver.com>") using a regex built by *me*. I want to learn that. I don't care if there is a module that already has this implemented. I don't care if there is a "generally accepted" way of doing it. I want to do it myself because I want to understand the full concept of it and then learn from it.

All I've wanted was some advices and tips on how to do it and some critcics on my regex.

So far in this node, only Ovid provided me that (with the link to the RFC and the addresses examples fom Mastering Regular Expressions).

Cheers,

Replies are listed 'Best First'.
Re^2: Please evaluate: RegEx for validating e-mail addresses
by FoxtrotUniform (Prior) on Sep 25, 2004 at 20:33 UTC

    Personally, I think you're tilting at windmills. That said, I can offer a small correction to Ovid's post: RFC 822 has been superseded by RFC 2822. (Since the RFCs provide what looks like BNF for the actual address spec, I'd suggest using a parser-builder instead of a regex - I guess Parse::RecDescent and Parse::YAPP are pretty good, though I haven't used either. Parsers are worth learning about, too.)

    --
    F o x t r o t U n i f o r m
    Found a typo in this node? /msg me
    % man 3 strfry

•Re^2: Please evaluate: RegEx for validating e-mail addresses
by merlyn (Sage) on Sep 25, 2004 at 20:28 UTC
    If you want to see how that regex was built, buy the MRE book. If you're just making toys to decorate your cubicle walls, fine. But if you're truly setting up a web page for general people to visit from other places, you must permit the RFC-permitted addresses. And the regex for that is the longish thing that Ovid posted. Nothing less.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I've only spent a few minutes looking for it, but my 2nd-edition Mastering Regular Expressions doesn't seem to include that regex. So I can't check my suspicion that the original MRE regex didn't actually match all RFC822 addresses, just most of them - I vaguely remember a disclaimer about comments in hostnames not being recognized, or some such. (Probably something recursive, now that I think about it.) Even Ovid's monstrous regex may not be complete.

      --
      F o x t r o t U n i f o r m
      Found a typo in this node? /msg me
      % man 3 strfry

        Yes, you remember almost correctly! The problem is nested comments. The oft-quoted monster regex deals with all comments, but no nested comments. Nested comments are impossible without resorting to trickery (a regex that only Perl could do because it has embedded Perl code).

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://393841]
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: (4)
As of 2024-03-29 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found