Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Sunday, April 15, 2012

Experimenting with precompiled regexes in Unquote

Today I spent some time experimenting with precompiled regexes in Unquote: https://unquote.googlecode.com/svn/branches/precompiled-regex. However, I was not able to show any measurable performance improvements from this work. Perhaps this was to be expected, considering Unquote only uses 5 compiled regexes currently and the upfront cost of compiling those on the fly must be small compared to the actual regex matching performed across all of Unquote’s nearly 500 unit tests (though Unquote uses a relatively small number of regexes, they are used frequently). So, for now, we’ll stick with normal compiled-on-the-fly regexes, unless we gain a large number of regexes in the future. But, it is worth noting the compiling on-the-fly does increase the performance of Unquote by 2x.

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"