Solved

Parsing email addresses

Posted on 2006-10-20
6
203 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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