2

How could I extract the email part from all of the following strings cases:

"Robert Donhan" <[email protected]>
Robert Donhan <[email protected]>
"Robert Donhan" [email protected]
Robert Donhan [email protected]

Thanks

2
  • Welcome to the site. The proper way to 'thank' someone on Stack Overflow is to up-vote the useful answers and accept the one that solved your problem in the end. Commented Mar 29, 2011 at 14:39
  • ...however, you need at least 15 reputation points to upvote. Accepting is always possible (click the checkmark on the left of the answer that helped you the most). Commented Mar 30, 2011 at 9:35

5 Answers 5

1

Could you try with this regex:

([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})

Anyway, I suggested this site: TXT2RE

Sign up to request clarification or add additional context in comments.

1 Comment

That site looks really awesome! Worth an upvote. I'm not sure how well it'd do in keeping in mind special characters that aren't in the default teststring, though.
1

Not completely correct but the following regex mostly works:

[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}  

with case insensitive option

Rubular Link

1 Comment

That {2,4} should be {2,6} (Don't leave out .MUSEUM and .TRAVEL!
0

Actually, your regex can be simpler. Split(explode) on the string using whitepace, go through each item, and check of "@". Then remove all < and >.

Comments

0

I think this should work:

/(^|\s)<?([a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z.]{2,6})>?(\n|\r|\s|$)/

Comments

0

If you do not necessarily need a regular expression, you could use imap_rfc822_parse_adrlist or mailparse_rfc822_parse_addresses to get both the address and the name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.