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: http://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:  http://amazon.com/dp/0764584367



// Shorten an Amazon.com URL


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


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


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

Comments (6)

  1. Oliver says:

    that’s great!

    will try to enhance my site with that code maybe

    amazon links are really too long

    http://truncurl.com 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(

     r'(?:http://(?:www.){0,1}amazon.com(?:/.*){0,1}(?:/dp/|/gp/product/))(.*?)(?:/.*|$)’,

     r’http://amazon.com/dp/1′,

     sys.argv[1]

    )

  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: amzn.com.

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

    http://amzn.com/0764584367

    Hopefully people find it useful.

  4. Adam Varga says:

    Some URL results returned by Amazon's API are formatted like this:

    http://www.amazon.com/…/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:

    (?:http://)(?:www.){0,1}amazon.(?:co.uk|com|de)(?:/.*){0,1}(?:/dp/|/gp/product/)(.*?)(?:/.*|$)

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

    great post, many thanks

  6. drcoopster says:

    You can shorten things even further:  amazon.com/…/0764584367 can become http://amzn.com/0764584367

Skip to main content