The desired result is a pattern for positive inputs that doesn't throw an exception in decimal.Parse
and doesn't accept the 01
-model-like inputs.
Valid inputs:
1
1.2
.2
1.
Invalid inputs:
01
01.2
.
1.B
A.2
A
A.
I liked the pattern in this answer (-?(0|([1-9]\d*))(\.\d+)?
); but somehow (as mentioned in the comment), it accepts X.2
(only with Regex.IsMatch
) and negative decimals, and rejects 1.
; so I modified it to /((0|([1-9]\d*))(\.\d*)?)|(\.\d+)/g
, and it worked perfectly in regexr.com and also RegExr v1; but I got no luck with Regex.IsMatch
.
Any suggestions? and why doesn't the pattern work with Regex.IsMatch
while it works somewhere else?