I am struggling to write the RegEx for the following criteria:
This is the regex I have tried to implement which does not work for me.
^((-?[0-9]{1,5}(\.?){1,1}[0-9]{0,2})
It should allow the user to type out the following numbers.
-1.12
12345
1
123
12.12
Any help would be appreciated!
You may use
^-?\d{0,5}(?:(?<=\d)\.\d{0,2})?$
See the regex demo.
Details
^
- start of string-?
- an optional -
\d{0,5}
- zero to five digits(?:(?<=\d)\.\d{0,2})?
- an optional sequence of
(?<=\d)
- there must be a digit immediately to the left of the current location\.
- a dot\d{0,2}
- zero, one or two digits$
- end of string.