Reputation: 1194
i want to parse numbers with unit like
3cm
3.44 cm
12,33 mm
the following expression does exactly what i want
(([0-9]*(?:[,.]?[0-9]*?))[\s]*(cm|mm))
but the problem is it also matches things like
cm 22.1 cm
in this case it ignores the number and matches the first cm only, how do i ignore the first cm?
Upvotes: 0
Views: 41
Reputation: 174706
Like others said, you need to change the quantifier *
to +
where *
represents zero or more and +
represents one or more. If you want to match also the numbers like 12,533.45 cm
, then you could use the below regex.
[0-9]+(?:,[0-9]+)*(?:\.[0-9]+)?\s*(?:cm|mm)
Upvotes: 1
Reputation: 24901
You can update the first number match quantifier from zero or more (*) to one or more (+):
(([0-9]+(?:[,.]?[0-9]*?))[\s]*(cm|mm))
Upvotes: 2
Reputation: 67968
\d+(?:[.,]\d+)?\s*(?:cm|mm)
Try this.See demo.
https://regex101.com/r/qJ8qW5/2
Upvotes: 1