0

I need a little assistance getting email addresses only from within their POSSIBLY INCLUDED <> brackets.

For example I have the following 3 strings and I need each one to return only the email address:

darth@vader.com

"Darth Vader" <darth@vader.com>

"Darth Vader" <darth@vader.com> "Possible additional text" (Shouldn't be here but I need to make sure the regex gets rid of it anyway just in case.)

On every single one of those I would want $email to equal darth@vader.com

Scott Rowley
  • 486
  • 1
  • 7
  • 30
  • Related: [Regular expression to validate full name with email in brackets (brackets required)](https://stackoverflow.com/q/26764944/2943403) and [How to get the text between angular brackets but without the angular brackets?](https://stackoverflow.com/q/57097696/2943403) and [Regular Expression to extract strings between angle brackets with exactly one @ char in between](https://stackoverflow.com/q/68491467/2943403) – mickmackusa May 02 '23 at 23:27

1 Answers1

4

How about just matching for valid e-mail addresses? The regex we use to check validity is:

/(([a-z0-9!#$%&*+-=?^_`{|}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+(com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum|co|co\.uk)/img

Or here's one that's completely TLD-agnostic:

/(([a-z0-9&*\+\-\=\?^_`{|\}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+([a-z]{2,})/img

One of those should work for what you're looking for and should cover most cases.

Makoto
  • 104,088
  • 27
  • 192
  • 230
Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • Jimmy, thanks for the fast response. What I would like is something that I ideally wouldn't ever have to change. The example you give (I think) identifies all current email extensions. One problem is that domain extensions have now been cleared to be nearly limitless. So that would completely ruin the above regex. I'd prefer something that grabs the <> and anything outside them and removes it. – Scott Rowley Aug 30 '11 at 15:17
  • You can certainly modify what I posted to not check for valid TLDs and instead just check for TLDs that are, you know, just *there*. Haha. I did an example of that [here](http://refiddle.com/1b6). – Jimmy Sawczuk Aug 30 '11 at 15:20
  • Jimmy, the one in your second reFiddle link is nearly perfect but its still including the left < Can you tell me how to get rid of that one as well? – Scott Rowley Aug 30 '11 at 15:27
  • Yep, try now. Needed to escape a few of those symbols. – Jimmy Sawczuk Aug 30 '11 at 15:34
  • Jimmy, would you know how I can use this in a preg_match? I tried several things but my variable always ended up empty. The following is what I've been going off of: http://php.net/manual/en/function.preg-match.php Example 3 – Scott Rowley Aug 30 '11 at 16:04
  • Actually nevermind, I removed the g from img and now it seems to be working just fine. – Scott Rowley Aug 30 '11 at 16:25