title: How write a regex that matches only when there's a slash OR nothing after the match?
body: <p>I'm trying to use <code>preg_match()</code> to extract 10-character ASIN numbers from Amazon URLs. The URLs could be in any of these basic formats:</p>
  
  <pre><code>http://www.amazon.com/gp/product/ASIN
  http://www.amazon.com/gp/product/[text]/ASIN
  http://www.amazon.com/o/ASIN
  http://www.amazon.com/dp/ASIN
  http://www.amazon.com/[text]/dp/ASIN
  http://www.amazon.com/[text]/dp/[text]/ASIN
  </code></pre>
  
  <p>NOTE: The problem I'm having stems from the fact that there may or may not be slashes and variables at the end of the URLs, after the ASIN.</p>
  
  <p>With the help I received in <a href="https://stackoverflow.com/questions/20984564/how-to-extract-data-from-urls-using-preg-match">a previous question</a>, I came up with this:</p>
  
  <pre><code>\/([A-Za-z0-9]{10})
  </code></pre>
  
  <p>Which I thought was working, until I tried it on this URL:</p>
  
  <pre><code>http://www.amazon.com/PlayStation-2-Console-Slim-Black/dp/B000TLU67W/ref=sr_1_4?ie=UTF8&amp;qid=1389314719&amp;sr=8-4&amp;keywords=playstation+1
  </code></pre>
  
  <p>The output of <code>preg_match()</code> for that is:</p>
  
  <pre><code>Array
  (
      [0] =&gt; /PlayStatio
      [1] =&gt; PlayStatio
  )
  </code></pre>
  
  <p>So then I tried adding a slash at the end of the regex, like this:</p>
  
  <pre><code>\/([A-Za-z0-9]{10})\/
  </code></pre>
  
  <p>Which fixes the problem, giving the following output for the above URL:</p>
  
  <pre><code>Array
  (
      [0] =&gt; /B000TLU67W/
      [1] =&gt; B000TLU67W
  )
  </code></pre>
  
  <p>However, there won't always be a slash at the end of the URL. For example, the above URL works just fine on Amazon if modified to this:</p>
  
  <pre><code>http://www.amazon.com/PlayStation-2-Console-Slim-Black/dp/B000TLU67W
  </code></pre>
  
  <p>My modified regex doesn't work for this URL, because there's no slash on the end.</p>
  
  <p>I think maybe having an OR condition to see if there's either a slash after the match, or nothing after it, might work, but I'm not sure how to do it..</p>
  
  <p>Is there any way to get the regex to work with both of the above URLs? </p>