[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Parsing email addresses

Posted on 2006-10-20
6
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1440 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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