Limit digits of whole/integral part of a decimal number using regex
Solution 1:
Try this regex:
^[1-9]\d{0,3}(?:\.\d{1,2})?
Click for Demo
Explanation:
-
^
- matches the start of the string -
[1-9]
- matches a single digit in the range1
to9
-
\d{0,3}
- matches at-least 0 or at-most 3 occurrences of any digit -
(?:\.\d{0,2})?
- matches the optional decimal and fractional part of the number. The fractional part matched will have atleast 1 digit or at most 2 digits
Edit:
To extract 8.
for inputs 8.a
, you can change the regex to - ^[1-9]\d{0,3}(?:\.\d{0,2})?