Your pattern matches any sequence of digits not followed by a .
and another digit. In 1234.5
the substring 123
is not followed by a .
(because it's followed by a 4
), so it's a valid match.
Try using an end anchor ($
) to ensure that no additional characters appear after the matched string:
^[+-]?\d+$
If you need to allow characters following the matched string, you might try using a negative lookahead to ensure that the matched substring is not followed by a .
or a digit:
^[+-]?\d+(?![\d.])
To also match a string like 10.ToString()
, you could also use a negative lookahead, like this:
^[+-]?\d+(?!\.?\d)
And another strategy would be to use a positive lookahead, like this:
^[+-]?\d+(?=\.\D|[^.\d]|$)