Reputation: 1477
could anyone help me with a RegEx? I want to delete all unneccessary information in the artivle title of beverage like size, quantity, and so on.
I made this RegEx: /(?<!^)(\d{1,2}\s?x\s?)?\d{1,2}((,|\.)\d{1,2})?\s?L?/igU
On 25 Test Drink 60% 12x1,0L
it's matching the 5
the 25
.
On Test Drink 60% 12x1,0L
it's matching the 60
before the %
.
How can I avoid these example? I want to match only the 12x1,0L
. I had to put so many optional characters, because the data source is very inconsistent on the format for the quantity and size. It can also happen that the quantity is missing, because it's only a single bottle/can.
I've provided more example titles below and here: https://regex101.com/r/aQ2kO8/2
25 Test Drink 60% 12x1,0L
Test Drink 60% 2x0,5
Test Drink 3 x 0,75 L
Test Drink 4x0,75 L
Test Drink 5 x 0,75L
Test Drink 66 x 0,75
Test Drink 0,75
Test Drink 0.75
7UP 12 x 1 L
Could someone please help me with this issue?
Upvotes: 3
Views: 131
Reputation: 8413
You can use (?:\d+\s*x\s*)?(?:\d+[.,])?\d+\s*L?\s*$
as a search pattern, you have to set the m- and g-modifier, if you aply it to a multiline text. Also i-modifier might be needed if you don't care for the char-case of l
and x
(?:\d+\s*x\s*)?
optionally matches digits followed by an x
which might be surrounded by spaces(?:\d+[.,])?
optionally matches digits followed by a dot or comma\d+
matches one or more numbers\s*L?\s*
optionally matches a L
which might be surrounded by spaces$
matches the end of the line. From your description the things you want to match are always at the end, by using this we can avoid matching numbers in the drinks nameTake a look at a regex101 demo.
Upvotes: 1