Tuesday, April 10, 2012

Convert Outlook formatted email addresses to Gmail format

Recently, I wanted to send an email from my personal Gmail account to a group of folks at my office. At the office, we use Outlook. So within Outlook, I entered all the email address in the “To” box and pressed “Check Names” to assert their validity and convert them into Outlook’s canonical format. I then copied and pasted those email address into the body of an email and sent it to my Gmail account for future use.

But alas, when it came time to send the email from my Gmail account to the Outlook formatted email addresses I had collected earlier, I got a send error due to Gmail’s inability to parse Outlook’s format. I had about 15 email addresses in the list, and decided it would be more fun to parse out the email addresses using a regex than to manually extract them.

Of course, I chose F# for the job, since, besides generally being my favorite go-to language, it is perfect for these quick interactive scripting scenarios. The following is the code snippet I came up with, which worked nicely as advertised.

open System.Text.RegularExpressions
let raw = @"John, Smith-Anderson <JohnS@Company.com>, Reg, Ex <RegE@Company.com>, Hello, World <HelloW@Company.com>"
let regx = new Regex(@"<([^<>]*)>")
let matches = regx.Matches(raw)

matches
|> Seq.cast<Match>
|> Seq.map (fun m -> m.Groups.[1].Value)
|> String.concat "; "
//val it : string = "JohnS@Company.com; RegE@Company.com; HelloW@Company.com"

No comments:

Post a Comment