0

I'm receiving content such as:

"Jane Doe" <jane@doe.com>, "John Doe" <john@doe.com>

And I would like to extract the email addresses:

jane@doe.com, john@doe.com

Currently I have a regex like this:

/<(.*)>/

Which gets everything in between brackets, but it also gets things like:

jane@doe.com>, "John Doe" <john@doe.com

When I used an @ sign in the middle, it also didn't work quite right:

/<(.*)@(.*)>/

I could explode() based on the comma, but I'm thinking there ought to be a way for preg_match() to give me what I want. What am I missing in my regex?

NightHawk
  • 3,633
  • 8
  • 37
  • 56

4 Answers4

3

Your regexp is too greedy. Use /<(.*?)>/ instead, or better even something like

/<([^<>]+@[^<>]+)>/
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
3

I use this for matching emails, I know there are other expressions out there in the wild that may catch more email address but I find this one quick to type and so far suits all my needs, feel free to give it a try:

/\w+[\.\w]+@\w+[\.\w]+/

You may want to use this with preg_match_all to capture all the addresses

for example:

$string = '"Jane Doe" <jane@doe.com>, "John Doe" <john@doe.com>';
preg_match_all('/\w+[\.\w]+@\w+[\.\w]+/', $string, $matches);
print_r($matches);
Dale
  • 10,384
  • 21
  • 34
1

Easiest way is to match everything except the final delimeter:

/<([^>]*)>/
GavinCattell
  • 3,863
  • 20
  • 22
0

You are including too much with the dot. Try this:

/<([^>]*)>/
JoelFan
  • 37,465
  • 35
  • 132
  • 205