Rob
Rob

Reputation: 21

php regex to match pattern

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

Upvotes: 2

Views: 116

Answers (5)

Gumbo
Gumbo

Reputation: 655189

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.

Upvotes: 0

tvkanters
tvkanters

Reputation: 3523

I think this should work:

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

Upvotes: 0

codaddict
codaddict

Reputation: 454960

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

Upvotes: 1

kurumi
kurumi

Reputation: 25599

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

Upvotes: 0

Andrea Redshot
Andrea Redshot

Reputation: 25

Could you try with this regex:

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

Anyway, I suggested this site: TXT2RE

Upvotes: 1

Related Questions