Amazon Short URL RegEx

When you search, 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:

Short URL:

// Shorten an URL

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


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 = "$1";

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


The way I'm using this is I have a little app and assigned to a hotkey so when I copy a long 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).