Determine if 10 digit string is valid Amazon ASIN

For PHP, there is a valid regular expression for ASINs here.

function isAsin($string){
    $ptn = "/B[0-9]{2}[0-9A-Z]{7}|[0-9]{9}(X|0-9])/";
    return preg_match($ptn, $string, $matches) === 1;
}

Update, 2017

@Leonid commented that he’s found the ASIN BT00LLINKI.

Although ASIN’s don’t seem to be strictly incremental, the oldest non-ISBN ASINs do tend to have more zeros than newer ASINs. Perhaps it was inevitable that we’d start seeing ASINs with no zero padding (and then what, I wonder...). So we’re now looking for "B" followed by nine alphanumeric characters (or an ISBN) — unfortunately, the "loss" of that zero makes it a lot easier to get a false positive.

/^(B[\dA-Z]{9}|\d{9}(X|\d))$/

Original answer

In Javascript, I use the following regexp to determine whether a string is or includes what’s plausibly an ASIN:

/^\s*(B\d{2}[A-Z\d]{7}|\d{9}[X\d])\s*$/

or, without worrying about extra whitespace or capturing:

/^(B\d{2}[A-Z\d]{7}|\d{9}[X\d])$/

As others have mentioned, Amazon hasn't really revealed the spec. In practice I've only seen two possible formats for ASINs, though:

  1. 10-digit ISBNs, which are 9 digits + a final character which may be a digit or "X".
  2. The letter B followed by two digits followed by seven ASCII-range alphanumeric characters (with alpha chars being uppercase).

If anyone has encountered an ASIN that doesn't fit that pattern, chime in. It may actually be possible to get more restrictive than this, but I'm not certain. Non-ISBN ASINs might only use a subset of alphabetic characters, but even if so, they do use most of them. Some seem to appear more frequently than others, at least (K, Z, Q, W...)

Tags:

Php

Amazon