http://www.perlmonks.org?node_id=79891

web-yogini has asked for the wisdom of the Perl Monks concerning the following question:

Namaste fellow seekers of wisdom!

I am having some troubles with one more "Regular Expression", which I really don't find regular at all!

I am trying to strip just "emailuser@emailaddress.com" from an email address that looks like this:
email user  <emailuser@emailaddress.com>

Thank you ever so much more so

Sad Gurunath Maharaj ki jai!

Replies are listed 'Best First'.
Re: Expressing myself
by TStanley (Canon) on May 12, 2001 at 05:40 UTC
    A quick search turned up this node that should help you get started.

    TStanley
    In the end, there can be only one!
Re: Expressing myself
by Kickstart (Pilgrim) on May 12, 2001 at 05:37 UTC
    Just this should work I think:

    m/.*?\<(.*?)\>/; print $1;

    Kickstart

      .*? is at least better than .*, but I prefer to be a little more explicit and would use something like <([^>]+)> instead.

      Something you also might want to watch out for is your use of $1 without verifying the match actually succeeded, leaving you with something you probably didn't expect.

          --k.


        Just out of curiosity, I benchmarked this.

        #!/usr/bin/perl -w use Benchmark; $email = '<silvers@op.net>'; Benchmark::cmpthese(100000000, { '.*' => sub { $email =~ m/.*?\<(.*?)\>/ }, '^>' => sub { $email =~ m/<([^>]+)>/ } } );

        Results:

        Benchmark: timing 100000000 iterations of .*, ^>... .*: 1437 wallclock secs (1432.85 usr + 0.06 sys = 1432.91 CPU +) @ 69788.05/s (n=100000000) ^>: 645 wallclock secs (645.23 usr + 0.06 sys = 645.29 CPU) @ + 154969.08/s (n=100000000) Rate .* ^> .* 69788/s -- -55% ^> 154969/s 122% --

        - FrankG

Re: Expressing myself - patterns for e-mail adresses
by bjelli (Pilgrim) on May 12, 2001 at 12:25 UTC

    I just realized I don't know if < needs to be escaped in patterns. I have this nagging feeling it should be, but I can't find anything in perlre.

    Hm, this seems to work ok:

    if (m/<(.*?)>/) { my $email = $1; if ($email =~ /@.*\./) { # ok, do something with it. } }

    I first find stuff in < > (don't do it this way if you're parsing html, you'll match all the tags). Then I have a look inside, to see if there's a @ and a dot. I am wary of more elaborate checking for e-mail adresses - you always end up discarding some valid adresses.

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
Re: Expressing myself
by eejack (Hermit) on May 12, 2001 at 05:49 UTC
    Perhaps this will help you on your way, (though I am sure there are brief/better ways)...
    #!/usr/bin/perl -w $test = 'email user <emailuser@emailaddress.com>'; $test =~ /\<([\.-A-Za-z]+\@[-A-Za-z]+\.[A-Za-z]+)\>/; print $1;

    Hope this helps,

    EEjack

      A very common fallacy is that the local-part ( the part to the left of the @) has a limited character set. I'm pretty amazed at how far that misinformation has seem to spread. My friend Eli, with a perfectly legal email address of <*@qz.to> would clearly object to you not seeing his address. And <fred&barney@stonehenge.com> is a perfectly legal email address: try sending a message to it!

      The proper answer is not to try to match the local-part with a small regex, but instead to use modules like Email::Valid and RFC::RFC822::Address which actually look at the specs instead of propogating a myth based on only what you've seen on your limited exposure to the net.

      -- Randal L. Schwartz, Perl hacker

        Thank you Randal

        I tend to avoid solutions based on multiple modules if possible, and in my limited exposure to the net I have found that simple, generalized, solutions based on real world situations tend to work very nicely, especially if you mark the pieces that do not conform.

        Having had problems getting the required modules working properly on some win32 and mac systems, I tend to avoid them.

        Yes, your friends' email address does indeed work, and is indeed valid, as would an entire list of others I have, maintain, and work with, and will not pass my simple (and admittedly ficokta) solution.

        However, I would point out - *in general* - folks with addresses specifically designed for obfuscation tend to not want their email addresses to pass.

        BTW - It's refreshing to know you are still you after all these years...keep the faith.

        EEjack

      What about <root@astlavista.box.sk>?

      Remember, even if an email address passes a regex, you do not necessarily know that it is indeed a VALID email address.

      Email capt3043@yahoo.com if you don't believe me.

        I agree it is not a perfect solution.

        To account for all the possible email addresses would be very difficult, made improbably more twisted trying to verify them as you point out.

        The closest I have gotten was a regex that pulled out the email, then attempted to send mail to the email account and stopping at the point where the smtp server would accept data and dropping the connection. I did this because so many mail servers were denying vrfy.

        However - now many mail servers are accepting *any* mail, then deciding whether or not to deliver or return. AOL is the big one doing this. I have my mail servers set up to do this as well on some domains (using ruleset 6 IIRC) and catchalls that dump non-valid email into the bit bucket on my ntmailserver.

        And honestly, I found my method, while functional, intrinsictly rude - so I stopped using it.

        The very best way to verify is to try your best in parsing, send an email for validation and keep track of responses and bounces.

        But thank you for bringing it up. The person who was asking didn't get my best answer, instead got my quick answer.

        Perhaps a better example with better explanations would have been more appropriate on my part - or a link to one of the many other places that would explain it better than I could. Unfortunately I have a difficult time explaining things well. It's a learning experience for everyone...:)

        Until I can dig up a link or two...

        #!/usr/bin/perl -w @test = ( '0email user <emailuser@emailaddress.com>', '1email user <email.user@emailaddress.com>', '2email user <emailuser@email-address.com>', '3email user <emailuser@mail.email-address.com>', '4email user <email.user@email-address>', '5email user <email.user@email.addres.scom>', '6email user <email.user@emailaddresscom>' ); foreach $temp (@test){ if ($temp =~ /\<([.-A-Za-z]+\@[.-A-Za-z]+\.[A-Za-z]{2,4}})\>/){ print "$1\n"; } else { print "$temp failed \n"; } }
        This will account for *many* of the common email address forms you will run into (using the formatting provided - there as many possible formats for an email address as there are ..umm.. email addresses). It doesn't account for lots of things, and as you can see, gives you a false positive for #5, but will get your list *real close*.

        Thanks again for the gentle reminder jeffa. Much appreciated.

        EEjack