in reply to Re: special handling with regular expressions
in thread special handling with regular expressions

No. See the other correct answers in this thread, particularly the ones that refer to CPAN modules.

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

  • Comment on •Re: Re: special handling with regular expressions

Replies are listed 'Best First'.
Re: special handling with regular expressions
by dave_the_m (Monsignor) on May 10, 2004 at 14:57 UTC
    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.

        Given that this field is overloaded to take either a description or an email address, I have a feeling that the "correct" regexp is going to treat some things as email addresses that weren't intended as such. And it sounds as if the primary use of the field is for descriptions, so it may be wise to err the other way.

        Update: an example would be:

        I also notified the guys at <foo@example.com>
Re: •Re: Re: special handling with regular expressions
by data67 (Monk) on May 10, 2004 at 14:55 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.