Regex that accepts floating point numbers and minus (-) sign

后端 未结 2 1019
小蘑菇
小蘑菇 2020-12-06 10:58

I want a regular expression that will accept only floating point numbers from 0 to 9 and minus sign.

Please help.

2条回答
  •  甜味超标
    2020-12-06 11:27

    ^[-+]?[0-9]*\.?[0-9]+$

    • ^ - start of string
    • [-+]? - 0 or 1 sign indicator
    • [0-9]* - 0 or more integers
    • \. - the character . (. is used in regex to mean "any character")
    • [0-9]+ - 1 or more integers
    • $ - the end of the string

    If you are instead using the comma as a decimal seperator, use , instead of \.

    If you are using both/either, you can use [.,]

提交回复
热议问题