Regex for numbers only

后端 未结 18 2292
野性不改
野性不改 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:12

    Here is my working one:

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

    And some tests

    Positive tests:

    string []goodNumbers={"3","-3","0","0.0","1.0","0.1","0.0001","-555","94549870965"};
    

    Negative tests:

    string []badNums={"a",""," ","-","001","-00.2","000.5",".3","3."," -1","--1","-.1","-0"};
    

    Checked not only for C#, but also with Java, Javascript and PHP

提交回复
热议问题