in reply to special handling with regular expressions

if ($text =~ /^\s* \S+ \@ \w+ [\.\w]* \s*$/x) { .... }

Replies are listed 'Best First'.
•Re: Re: special handling with regular expressions
by merlyn (Sage) on May 10, 2004 at 14:48 UTC
      Yes, Merlyn. but i can't restrict myself to a any perl module b/c i know this reqular expression will be ported to java later on.
        If you can't use the Perl modules directly, then lift the regex from the modules. However, it's quite long, and it's quite long for a reason. Be sure you read my other posts in this thread to avoid disinformation and misinformed people who think they are helping.

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

      I don't think the idea of handling form text value differently if it looks like an email addess is particularly wise, but if the OP is determined to go with such an approach, then my regex above is as good a heurstic as anything else. Sure, you could use a CPAN module that can correctly parse RFC822 addresses, but since we are dealing with a web form where users have typed in stuff by hand, its fairly unlikely they're typing the full RFCish stuff involving quoting, <> etc.
        Your regex rejects:
        "Randal L. Schwartz"@vms-relay.stonehenge.comm
        And if that was my legitmate email address because of the way my internal company mailing system was set up, you've now given me no opportunity to use your web site. You made two common mistakes in your regex:
        • \w doesn't match hyphen, and yet hyphen can easily appear in a domain name.
        • Legitimate email addresses might have embedded spaces.
        The proper answer here is to not make things up and use the FAQ answers. Don't just invent your own thing.

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