Question

I'm about to break this down into two operations since I can't seem to figure out the regular expression to do it in one. However, I thought I would ask the brain trust here to see if anyone can do it (which I'm sure someone can).

Essentially I have a string containing a recipients field from an email in Exchange. I want to parse it out into individual recipients. I don't need to validate emails or anything. Essentially the data is comma separated except if the comma is in between a set of quotes. That's the part that's messing me up.

Right now I'm using: (?"[^"\r\n]*")

Which gives me the quoted names, and ([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})

which gives me the email addresses

Here's what I have.. Data:

"George Washington" <gwashington@government.net>, "Abraham Lincoln" <alincoln@government.net>, "Carter, Jimmy" <jimmy.carter@presidents.com>, "Nixon, Richard M." <tricky.dick@presidents.com>

What I'd like to get back is this:

"George Washington" <gwashington@government.net>
"Abraham Lincoln" <alincoln@government.net>
"Carter, Jimmy" <jimmy.carter@presidents.com>
"Nixon, Richard M." <tricky.dick@presidents.com>
Was it helpful?

Solution

I dont know enough about the exchange to get the pattern that will match for any exchange recipients entries.

But based on information past for you as an example. I give you this:

["][^"]+["][^",]+(?=[,]?)

This match all for entries that you post.

And know a simple example in C# how to use:

var input = "\"George Washington\" <gwashington@government.net>, \"Abraham Lincoln\" <alincoln@government.net>, \"Carter, Jimmy\" <jimmy.carter@presidents.com>, \"Nixon, Richard M.\" <tricky.dick@presidents.com>";

var pattern = "[\"][^\"]+[\"][^\",]+(?=[,]?)";

var items = Regex.Matches(input, pattern)
    .Cast<Match>()
    .Select(s => s.Value)
    .ToList();

If there is a input text that this pattern dont work please post the input here.

OTHER TIPS

Regex.Match(input, @"\"[^\"]*\"\s\<[^>]*>");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top