共用方式為


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).

Comments

  • Anonymous
    February 15, 2008
    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

  • Anonymous
    April 26, 2009
    The comment has been removed

  • Anonymous
    December 30, 2009
    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.

  • Anonymous
    June 25, 2010
    The comment has been removed

  • Anonymous
    January 06, 2011
    The comment has been removed

  • Anonymous
    March 27, 2011
    The comment has been removed