regex for only numbers in string?

后端 未结 3 1534
野性不改
野性不改 2021-01-19 09:10

I can\'t find the regex for strings containing only whitespaces or integers. The string is an input from user on keyboard. It can contain everything but \\n (bu

相关标签:
3条回答
  • 2021-01-19 09:13

    How about something like this

    ^ *\d[\d ]*$
    

    See demo at regex101

    The pattern requires at least one digit to be contained.

    0 讨论(0)
  • 2021-01-19 09:18
    >>> re.match(r'^([\s\d]+)$', text)
    

    You need to put start (^) and end of line ($) characters in. Otherwise, the part of the string with the characters in will match, resulting in false positive matches

    0 讨论(0)
  • 2021-01-19 09:28

    To match only a whitespace or a digit you could use:

    ^[ 0-9]+$

    That would match from the beginning of the string ^ one or more whitespaces or a digit using a character class [ 0-9]+ until the end of the string $.

    0 讨论(0)
提交回复
热议问题