How I parse pack sizes like 6x0,5l, 25 dag and Lt 2,0X6 across 27 European countries
Описание и применение
Парсер преобразует названия товаров из 27 европейских стран в единый базовый размер (граммы, миллилитры или штуки), позволяя точно рассчитывать цену за единицу и выявлять сокращение объёма.
Задача
Parsing product pack sizes from diverse European shop titles into a single base unit (grams, millilitres or pieces) to compute unit prices and detect shrinkflation.
Результат
The parser accurately interprets diverse European pack size notations, enabling consistent unit price calculations and shrinkflation alerts across 27 countries.
Рабочий процесс
- Define numeric pattern accepting both '.' and ',' separators.
- Create a regex that matches a number followed by a unit, ensuring the unit is not part of a larger word via negative lookahead for Latin, Cyrillic, and Greek letters.
- Build a unit table mapping many spellings to a base unit and conversion factor.
- Implement multipack regex to capture patterns like '6x0,5l' or '6 × 500 ml' before single sizes.
- Parse titles using the multipack regex first; if no match, use the single-size regex.
- Convert parsed values to base units using the unit table.
- Compute unit price as price / amount.
- Integrate parser into the ShrinkWatch EU extension to display price per kg/ml/pc and shrinkflation warnings.
Доказательства
ShrinkWatch EU is a free browser extension. On grocery web shops it shows the price per kg, litre or piece, and it warns you when a pack gets smaller for the same price.
parseSize('Pivo 6x0,5l') // { amount: 3000, unit: 'ml', count: 6, each: 500 }
The number pattern accepts either separator, and a size only counts when a unit follows it right away.
Units come in many spellings. All of them map to a base unit and a factor: ...
6x0,5l and 6 × 500 ml mean 3 litres. These are matched before single sizes:
- Дата записи: 2026-10-20T09:41:47.632Z