in reply to pattern match e-mail addresses

I think the question is subtly different from what we've all been answering. Fian didn't ask how to validate an email address, but rather how to match an email address.

In other words, given a file, print everything that looks like an email address. That's a subtly different question from "is this a valid mail address", or even from "Here's a line with a mail address in it, please parse out the relevant bits."

Embedded newlines could wreak even more havoc.

A lot depends on the dataset, and how you define what addresses you're looking for. For example, barewords are syntactically invalid mail addresses, but if I open an xterm and type "mail postmaster" it will probably get delivered.

This is a stickier problem than it appears. You might try using the 822-valid beast from Mastering Regular Expressions, which you can find here.

And please drop a note to tell us you're on the side of good, and aren't trying to scrape email addresses off a website or something.

Peace,
-McD

Replies are listed 'Best First'.
(dkubb) Re: (2) pattern match e-mail addresses
by dkubb (Deacon) on Mar 23, 2001 at 07:27 UTC

    There is a CPAN module that will match email addresses inside a string; Email::Find. Here's an example using this module:

    #!/usr/bin/perl -w use strict; use Email::Find qw(find_emails); use vars qw(@MATCHES); my $text = 'user@somewhere.com not an email address me@home.com'; find_emails($text, \&callback); print join "\n", @MATCHES; sub callback { my $email = shift; my $original = shift; push @MATCHES, $email->format; return $original; }

    Warning: the find_emails() routine will modify the original text. Make sure that you always return the original email text (as shown above), so that your input text does not mysteriously change.

Re: Re: pattern match e-mail addresses
by Fian (Novice) on Mar 23, 2001 at 16:00 UTC
    :-)
    It never even crossed my mind that u might think I was up to no good...
    This is part of a project I'm doing at work to take a load of registrations from a web form and parse the data into a comment delimited file for someone else to then read into an access database...
      First of all thank u for all the help.

      I now have a lot of stuff to keep me busy for a quite a while...
      This is THE best site I have found for all things Perly,

      Keep 'er Lit....

      Fian (Belfast, N.Ire.)