Regex for numbers only

后端 未结 18 2265
野性不改
野性不改 2020-11-22 03:29

I haven\'t used regular expressions at all, so I\'m having difficulty troubleshooting. I want the regex to match only when the contained string is all numbers; but with the

18条回答
  •  不思量自难忘°
    2020-11-22 04:24

    Regex for integer and floating point numbers:

    ^[+-]?\d*\.\d+$|^[+-]?\d+(\.\d*)?$
    

    A number can start with a period (without leading digits(s)), and a number can end with a period (without trailing digits(s)). Above regex will recognize both as correct numbers.

    A . (period) itself without any digits is not a correct number. That's why we need two regex parts there (separated with a "|").

    Hope this helps.

提交回复
热议问题