Regex for numbers only

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

    Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

    regex = new Regex("^[0-9]+$");
    

    The ^ will anchor the beginning of the string, the $ will anchor the end of the string, and the + will match one or more of what precedes it (a number in this case).

提交回复
热议问题