re = /(?:o|dp(?:\/[^\/]+)?|gp\/product(?:\/[^\/]+)?)\/([A-Z0-9]{10})/mi
str = 'http://www.amazon.com/PlayStation-2-Console-Slim-Black/dp/B000TLU67W/ref=sr_1_4?ie=UTF8&qid=1389314719&sr=8-4&keywords=playstation+1
http://www.amazon.com/PlayStation-2-Console-Slim-Black/dp/B000TLU67W
http://www.amazon.com/gp/product/C0FF33CUP5
http://www.amazon.com/gp/product/CoffeeCups/C0FF33CUP5
http://www.amazon.com/o/C0FF33CUP5
http://www.amazon.com/dp/C0FF33CUP5
http://www.amazon.com/CoffeeCups/dp/C0FF33CUP5
http://www.amazon.com/CoffeeCups/dp/some-text/C0FF33CUP5
http://www.amazon.com/CoffeeCups/dp/B000TLU67W/ref=sr_1_4?ie=UTF8&qid=1389314719&sr=8-4&keywords=playstation+1'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html