Amazon identifies every product for sale with a ten-digit alphanumeric code called an ASIN (Amazon Standard Identification Number). Whenever you visit a product page on Amazon, these number is somewhere in the URL; however, it's not always in the same place in the URL. This makes it a little bit hard to pull the ASIN out of an arbitrary URL.

There are many ways to solve this problem. I choose to use a tests and Ruby regular expressions. This may not be the best way to solve this problem, but it works for me. If you'd like to learn more about Ruby, I recommend reading "The Ruby Programming Language: Everything You Need to Know".

Problem Solving Methodology

First, I went around Amazon clicking on as many links and products as I could to find as many different link formats as possible. I then used those links to build the test below. This solution is part of a Ruby on Rails application, so I show the file location of each code snippet as a comment at the top of the code.

Notice that I also included a false test case at the end because I don't want to return an ASIN if the string being processed isn't an Amazon URL. Also, the ASINs in the test have been modified slightly (I changed the first letter of each one) to make it easy to figure out which of the test cases was failing.

Once I had my tests set up, it was pretty easy to just go to town on the regular expressions until I got something that both made sense and passed my tests: