0

I need a regex which match in all cases the associate email address of the from 'tag'.

Examples:

From: =?iso-8859-1?Q?First_Second_=28Company_Inc=29?=
    <first.second@email.com>

From: =?iso-8859-1?Q?First_Second_=28Company_Inc=29?=
 <first.second@email.com>

From: =?iso-8859-1?Q?First_Second_=28Company_Inc=29?=
 first.second@email.com

From: =?iso-8859-1?Q?First_Second_=28Company_Inc=29?=
    first.second@email.com

From: first.second@email.com

From: <first.second@email.com>

I tried different patterns (RegexOptions.Multiline) but probably took a false approach. The one listed below matches not to the second to last.

^From: ([^<>\r\n]+([ |\r\n|\r|\n][\t| ])?)?[<]?([^<>]+)[>]?$

Thanks in advance.

Kinde regards,
Danny

dannyyy
  • 1,784
  • 2
  • 19
  • 43
  • Do you want to match everything after the `From: `, or just the email address `first.second@email.com`? – hochl Nov 24 '11 at 11:59
  • just the email address. if it's easier to match the whole part (including the >) then it's also an acceptable solution (calling trim() will do the job). – dannyyy Nov 24 '11 at 12:16
  • look at http://stackoverflow.com/q/210945/589206 for a complete answer! :) – hochl Nov 24 '11 at 12:21
  • these lines are snippets of a complete email header. i don't need to validate whether the email address is correct or not. i need the sender address of an email. searching for valid email address in a email header will result in countless matches. – dannyyy Nov 24 '11 at 12:48

1 Answers1

1

After some further try and error I got the pattern

From:( [^<>\r\n]+[ |\r\n|\r|\n]?)?[\t| ]+[<]?([^<>]+?@[^>\r\n]+)[>]?
dannyyy
  • 1,784
  • 2
  • 19
  • 43