Parse Name <Email>

S

Shapper

Hello,

I have a method which receives a contact in one of the following formats:

1 - "(e-mail address removed)"

2 - "Name <[email protected]>" OR "Name<[email protected]>" (Spaces can exist)

If it is in format (1) I do nothing. In case of (2) I need to parse the name and email.

I never know in which format I will get the emails. But it will be one of the two.

How can I do this?

Thank You,

Miguel
 
A

Arne Vajhøj

I have a method which receives a contact in one of the following formats:

1 - "(e-mail address removed)"

2 - "Name <[email protected]>" OR "Name<[email protected]>" (Spaces can exist)

If it is in format (1) I do nothing. In case of (2) I need to parse the name and email.

I never know in which format I will get the emails. But it will be one of the two.

How can I do this?

Regex!

Arne
 
A

Arne Vajhøj


Something like:

private static readonly Regex re = new Regex(@"([^@<>]+@[a-z0-9\-.]+)",
RegexOptions.Compiled);
public static string FindEmail(string addr)
{
return re.Match(addr).Value;
}

You may need to adjust what is legal characters.

Arne
 
S

Shapper

On 9/15/2012 5:36 PM, Shapper wrote:

Regex!



Something like:



private static readonly Regex re = new Regex(@"([^@<>]+@[a-z0-9\-.]+)",

RegexOptions.Compiled);

public static string FindEmail(string addr)

{

return re.Match(addr).Value;

}



You may need to adjust what is legal characters.



Arne

Hello Arne,

I am not trying to take the email.

I am trying to take the Name and the Email.

Something like:

"(e-mail address removed)" > "John Smith<[email protected]"

"John Smith<[email protected]>" > "John Smith", "(e-mail address removed)"

"John Smith <[email protected]>" > "John Smith", "(e-mail address removed)"

Thank You,
Miguel
 
A

Arne Vajhøj

I am not trying to take the email.

I am trying to take the Name and the Email.

Something like:

"(e-mail address removed)" > "John Smith<[email protected]"

"John Smith<[email protected]>" > "John Smith", "(e-mail address removed)"

"John Smith <[email protected]>" > "John Smith", "(e-mail address removed)"

You can not in general get the full name from the email
address.

It is possible to grab whatever is in front of <.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top