I'm writing a script in Powershell to take a list of email recipients in the general format """Name One"" <email one>, ""Name Two"" <email two>"
and output a list of recipients that can be parsed as a CSV (outside Powershell). The list comes from a cell in a CSV that I cannot control the format of. I can't parse the list itself as a CSV directly: because there's no specific number of recipients per email, I can't specify a header, and attempting to parse it as a CSV by removing the initial and ending quote results in improperly quoted names (and potential problems when the name contains a comma).
So far, I've realized that I can load the string as a CSV and that will turn the above example into "Name One" <email one>, "Name Two", <email two>"
, and this would be perfect if I could split on the comma. Unfortunately I cannot, since sometimes people's names show up as "Three, Name"
and I can't split in the middle of a person's name. What I need is some way I can replace the comma outside the quotes with some other character (or series of characters). If I can do that, then the rest of my process will work flawlessly; as it is, I'm running into issues because the quickly thrown together regex I was using doesn't properly catch recipients without an email address.
The only idea I have at the moment is to write a parser that steps through each character, keeps track of how many quotes it comes across, and replaces the comma when that number is even. I'd rather not do this; there's a lot of data and I'd rather something faster if possible. I'll implement it that way for now, but I'm hopeful I can get a better solution here.
Edit: To clarify on the format: most emails are in the format ""Something"" <addr@example.org>
but not all are. The most common format other than that is just a name (and nothing in angle brackets), but outside of that most of the emails do follow that format of something in double quotes followed by something in angle brackets. I can't really pull out all the different formats; I've got too many messages and only a handful are exceptions.