Amazon Short URL RegEx

When you search amazon.com, it takes you to product pages that have a real long URL.  It can be nice and short, so here's a little regular expression to shorten it.

Long URL: https://www.amazon.com/Professional-Visual-Studio-System-Programmer/dp/0764584367/ref=sr_1_1/104-4732806-7470339?ie=UTF8&s=books&qid=1179873697&sr=8-1

Short URL:  https://amazon.com/dp/0764584367

// Shorten an Amazon.com URL

RegEx: (?:https://(?:www\.){0,1}amazon\.com(?:/.*){0,1}(?:/dp/|/gp/product/))(.*?)(?:/.*|$)

Replace: https://amazon.com/dp/$1

A little code to change the URL in the clipboard.

string longurl = Clipboard.GetText();

string pattern = @"(?:https://(?:www\.){0,1}amazon\.com(?:/.*){0,1}(?:/dp/|/gp/product/))(.*?)(?:/.*|$)";

string replacement = "https://amazon.com/dp/$1";

string shorturl = Regex.Replace(longurl, pattern, replacement);

Clipboard.SetText(shorturl);

The way I'm using this is I have a little app and assigned to a hotkey so when I copy a long amazon.com url, I hit my hotkey and it's made short in the clipboard (using WinKey).

BTW, I am a very amateur regex coder so it may not be optimized.  I use RegexBuddy to test and MSDN on RegEx as my reference.

Update (on 5/15/08): I've found several more test cases recently and have updated the regex code accordingly.  The URLs are also 4 characters shorter now (www. not nessasary, duh).