re = /.*(?:\d(?:\.\d+)?\s*x\s*)?\K(?<!\d)\d+(?:\.\d+)?(?:k?g|m?l)\b|(?<!\d)\d+(?:\.\d+)?(?:k?g|m?l)(?=\s*x\s*\d)/i
str = 'product brand A 1237ml Bundle of 6
product milk choc370ML
brand milk Vanilla Flavor 850g
One 2400g, For 0-6 Month-Old Infants
a+...two...6-12months...11.2kg...milk
a+...two...11.2kg 6-12months ..milk
Product 200g (10x2g)
[200g] Product
Product A brand(300g)
Product 200g (20g x 10)'
# 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