Solved

Parsing email addresses

Posted on 2006-10-20
6
200 Views
Last Modified: 2010-04-16
I need to parse a string like this :

<email@domain.com>,"first name" <email@domain.com>,"firstname lastname" <email@domain.com>, "firstname, lastname" <email@domain.com>,email2@domain.com

So basically, I need to get all emails, and of course the corresponding name if any.

The problem is in the fourth email. You see, the guy put a , in his name, so parsing using .Split(',') will not work.

I'm searching for a solution to be able to get both name and address in all cases.

Thanks  a lot!
0
Comment
Question by:pmengal
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 17775309
string input = "<email@domain.com>,\"first name\" <email@domain.com>,\"firstname lastname\" <email@domain.com>, \"firstname, lastname\" <email@domain.com>,email2@domain.com";
            MatchCollection matches = Regex.Matches(input, "(\"(?<name>.+?)\")*\\s*<(?<email>.+?)>");
            foreach(Match m in matches)
            {
                Console.Write("Name: " + m.Groups["name"].Value + "       ");
                Console.WriteLine("Email: " + m.Groups["email"].Value);
            }
0
 

Author Comment

by:pmengal
ID: 17776033
Almost ! The last email was not caught. Because it hasn't the < >
0
 
LVL 13

Expert Comment

by:joechina
ID: 17778749
This will work. But I will post a simpler one later

string input = "<email@domain.com>,\"first name\" <email@domain.com>,\"firstname lastname\" <email@domain.com>, \"firstname, lastname\" <email@domain.com>,email2@domain.com";
            MatchCollection matches = Regex.Matches(input, "(\"(?<name>.+?)\")*\\s*<?(?<email>[a-zA-Z][\\w\\.-_]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-_]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z])>?");
            foreach (Match m in matches)
            {
                Console.Write("Name: " + m.Groups["name"].Value + "       ");
                Console.WriteLine("Email: " + m.Groups["email"].Value);
            }
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:pmengal
ID: 17779625
Ok thanks. I'm waiting for it. I will give you more points.
0
 
LVL 13

Accepted Solution

by:
joechina earned 360 total points
ID: 17781079
string input = "<email@domain.com>,\"first name\" <email@domain.com>,\"firstname lastname\" <email@domain.com>, \"firstname, lastname\" <email@domain.com>,email2@domain.com";
 MatchCollection matches = Regex.Matches(input, "(\"(?<name>.+?)\")*\\s*<?(?<email>[^<>,\"\\s]+)>?");
 foreach (Match m in matches)
 {
      Console.Write("Name: " + m.Groups["name"].Value + "       ");
      Console.WriteLine("Email: " + m.Groups["email"].Value);
  }

This is a simpler version. But previous regex will validate the email format. This one won't
0
 

Author Comment

by:pmengal
ID: 17783431
;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question