How can I write a regular expression that matches a string with the following properties?:
123 or 123.4 or 123.56)..12).000.12, only 0.12).To the left of the decimal point you want a number (1 or more digits) that doesn't start with a zero:
[1-9][0-9]*
Or it can be just a zero:
0|[1-9][0-9]*
The value may have a decimal point and 1-2 digits after the decimal point:
\.[0-9]{1,2}
Left side is required. Decimal point and fractional digits are optional:
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
The first non-capturing group is needed to limit the scope of the | pattern. The second non-capturing group is needed to make combined "decimal point and fractional digit" pattern optional.
Note that this will allow trailing zeroes, e.g. 100.00
Depending on preference, [0-9] can also be written as \d. I'd normally use \d, but since regex also has [1-9], I liked [0-9] better here as I felt it helped clarify the difference.
Depending on how regex is used, you may need to add the ^ begin / $ end anchors. They are needed when using find(), and are not needed when using matches() but don't hurt:
^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?$
Using negative look-ahead to ensure the string doesn't start with zero and another digit (but can be just zero, or zero followed by a dot)
^(?!0\d)\d+(?:\.\d{1,2})?$
Explanation and sample: https://regex101.com/r/7ymqcn/1
P.S. Also more efficient than Andreas' answer (takes fewer steps to match)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With