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:    (?:http://(?: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 = @"(?:http://(?: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).

Comments (6)

  1. Oliver says:

    that’s great!

    will try to enhance my site with that code maybe

    amazon links are really too long if you want to see my site

  2. Adam Monsen says:

    Very useful, thanks!

    Here’s a Python version:

    import re,sys

    print re.sub(





  3. Arturo says:

    I know this is an old post, but it’s the first Google result for "amazon shorten url", so I wanted to contribute another way to make even shorter Amazon URLs:

    You can take the product code and just tack it to the end of, like so:

    Hopefully people find it useful.

  4. Adam Varga says:

    Some URL results returned by Amazon's API are formatted like this:…/B0019VPL9O%3FSubscriptionId%3D0PGBCS7Y9Q61TZ2SN3R2%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0019VPL9O

    These URLs either need to be urldecoded or the regex modified the account for a trailing "%" in addition to a trailing "/"

  5. toast says:

    hey, found this on google when looking for something similar for Ruby, the following checks out ok on rubular:


    (oh, i allowed for, .com and .de in there as well)

    great post, many thanks

  6. drcoopster says:

    You can shorten things even further:…/0764584367 can become

Skip to main content