Python regex for integer?

后端 未结 4 967
面向向阳花
面向向阳花 2020-12-09 15:35

I\'m learning regex and I would like to use a regular expression in Python to define only integers - whole numbers but not decimals.

I could make one that only allow

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 16:04

    You need to anchor the regex at the start and end of the string:

    ^[0-9]+$
    

    Explanation:

    ^      # Start of string
    [0-9]+ # one or more digits 0-9
    $      # End of string
    

提交回复
热议问题