Regex matching numbers and decimals

后端 未结 5 1402
深忆病人
深忆病人 2020-12-06 10:53

I need a regex expression that will match the following:

.5
0.5
1.5
1234

but NOT

0.5.5
absnd (any letter character or space         


        
5条回答
  •  失恋的感觉
    2020-12-06 11:34

    Nobody seems to be accounting for negative numbers. Also, some are creating a capture group which is unnecessary. This is the most thorough solution IMO.

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

提交回复
热议问题