Solved

Parsing email addresses

Posted on 2006-10-20
6
197 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now