1

I thought I had it figured out, but it appears that my regex still has quirks in it. Basically I would like to use the same regex pattern to match the following major email clients (Gmail, Yahoo, and regular email):

"Brian Mang" <brian.mang@email.com>   -- Case1
Brian Mang (brian.mang@email.com)     -- Case2
<brian.mang@email.com>                -- Case3
brian.mang@email.com                  -- Case4

I had the following regex pattern:

/[\W"]*(?<name>.*?)[\"]*?\s*[<(](?<email>\w.*)[>)]/.match(contact)

and it works for all Cases 1-3, but I cant get it to pick up case 4, I tried messing around with it but cant figure it out cause it breaks the other cases. Any idea what I need to change/modify to make my regex pick up all of the 4 cases? Thank you.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

2 Answers2

2

Try this

[\W"]*(?<name>.*?)[\"]*?\s*[<(]?(?<email>\S+@\S+)[>)]?

See it here on Regexr

I made the classes surrounding the address optional and changed the part that matches the email to \S+@\S+ that means at least one non-whitespace followed by a @ then at least one more non-whitespace character.

Since the above version matches the closing character also, you can restrict the part after the @ a bit more

[\W"]*(?<name>.*?)[\"]*?\s*[<(]?(?<email>\S+@[^\s>)]+)[>)]?

see it here on Regexr

stema
  • 90,351
  • 20
  • 107
  • 135
  • Thanks @stema but it appears that the final puncuation >) still exist for Case1 & Case2 & Case4., for example: email brian.mang@email.com> has the punctuation at the end, i tried messing with it but cant figure out how to remove it? – Kamilski81 Feb 11 '12 at 23:36
-1

Edit: This one works for all four:

[\W"]*(?<name>.*?)[\"]*?\s*[<(]?(?<email>\S+@[^)>]+)[>)]?
user2398029
  • 6,699
  • 8
  • 48
  • 80
  • This breaks Case1 and Case2 for matching the name and email: For example name="" email="Brian Mang (brian.mang@email.com)" – Kamilski81 Feb 11 '12 at 22:14