in reply to Re: stripping out email address from string
in thread stripping out email address from string

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.

Replies are listed 'Best First'.
Re^3: stripping out email address from string
by JavaFan (Canon) on Aug 23, 2009 at 16:39 UTC
    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.