Assuming I have an Amazon product URL like so
http://www.amazon.com/Kindle-Wireless-Reading-Display-Generation/dp/B0015T963C/ref=amb_link_86123711_2?pf_rd_m=ATVP
Since the ASIN is always a sequence of 10 letters and/or numbers immediately after a slash, try this:
url.match("/([a-zA-Z0-9]{10})(?:[/?]|$)")
The additional (?:[/?]|$) after the ASIN is to ensure that only a full path segment is taken.
(?:[/?]|$)