blairanderson
9/22/2017 - 11:14 PM

Amazon ASIN regex and Ruby for plucking asins from a textarea that is comma separated or newline separated

Amazon ASIN regex and Ruby for plucking asins from a textarea that is comma separated or newline separated

class SomeController
  def asin_params
    # comma or semicolon, optionally surrounded by whitespace
    # or
    # two or more whitespace characters
    # or
    # any number of newline characters
    String(params[:asins])
      .split(/ \s*[,;]\s* | \s{2,} | [\r\n]+/x)
      .select { |item| item.length > 8 }
      .map(&:upcase)
  end
end