[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parsing email addresses

Posted on 2006-10-20
6
Medium Priority
?
209 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
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

834 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