ukndoit has asked for the wisdom of the Perl Monks concerning the following question:

We are trying to build an easier way for our club members to turn off their email notifications by 'unsubcribing' to our database. We send the email out with the email in this format:
$__emailAddressIs = "My Name <myemail@mydomain.com>";
However that is done way before we need the address for the link. So, we are doing this:
my $_sendTo = $__emailAddressIs; my $_trash; ($_trash,$_sendTo) = split /\</, $_sendTo, 2; ($_sendTo,$_trash) = split /\>/, $_sendTo, 2;
there must be an easier way to get that email address out of that string, right?

Thank you very much Xav

Replies are listed 'Best First'.
Re: stripping out email address from string
by jbt (Chaplain) on Aug 23, 2009 at 04:11 UTC
    One way to do it:

    $_sendTo =~ s/.*<([^>]*)>.*/\1/;
      Note that this will match any text between angle brackets. It will also malfunction when presented with multiple <s, which could be a problem if people are discussing math. (e.g., "5<3 according to Bob <bob@somewhere.com>" will give you "3 according to Bob <bob@somewhere.com", not just the actual email address.)

      Also note that leading and trailing .* on a regex is just meaningless noise, as the regex will match anywhere in the string by default.

      If I were to do this with just a plain regex, I'd go with

      $__emailAddressIs =~ /<([^<@>]+@[^<@>]+)>/; $_sendTo = $1;
      or, a little shorter (but less clear to the uninitiated),
      ($_sendTo) = $__emailAddressIs =~ /<([^<@>]+@[^<@>]+)>/;
      This will only match a sequence of <, followed by @, followed by >, with each symbol separated from the next by one or more characters other than those three symbols. It's still not perfect (using regexes to determine the validity of an email address is a fool's game), but should be close enough for most purposes.
        It's still not perfect (using regexes to determine the validity of an email address is a fool's game), but should be close enough for most purposes
        With 5.10 regexes, a regexp to match email addresses isn't too hard.

        However, a typical string contains many "valid" email addresses. See, according to the RFC, an email address doesn't really have to have an '@' sign. A string of ASCII letters is a valid email address. (Your typical MTA will try to deliver it locally). Just grabbing what's between < and > is more likely to produce what the user wants than throwing a correct email address regexp against it.

      Awesome. Thanks guys.

      That did it.

      Thank you very much Xav
Re: stripping out email address from string
by Anonymous Monk on Aug 23, 2009 at 04:11 UTC
    use Email::Address(); $_sendTo =~ s/$Email::Address::angle_addr//g;