Regex for positive float numbers

后端 未结 4 779
小蘑菇
小蘑菇 2020-11-30 08:56

For example:
10
0.1
1.23234
123.123
0.000001
1.000
.3

And wrong examples:
0001.2
-12
-1.01<

4条回答
  •  星月不相逢
    2020-11-30 09:38

    Try this here

    ^(?:[1-9]\d*|0)?(?:\.\d+)?$
    

    See it here online on Regexr

    If matching the empty string is not wanted, then you can add a length check to your regex like

    ^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$
    

    The positive lookahead (?=.+) ensures that there is at least 1 character

提交回复
热议问题