Python regex for integer?

后端 未结 4 958
面向向阳花
面向向阳花 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
    
    0 讨论(0)
  • 2020-12-09 16:04

    I prefer ^[-+]?([1-9]\d*|0)$ because ^[-+]?[0-9]+$ allows the string starting with 0.

    RE_INT = re.compile(r'^[-+]?([1-9]\d*|0)$')
    
    
    class TestRE(unittest.TestCase):
        def test_int(self):
            self.assertFalse(RE_INT.match('+'))
            self.assertFalse(RE_INT.match('-'))
    
            self.assertTrue(RE_INT.match('1'))
            self.assertTrue(RE_INT.match('+1'))
            self.assertTrue(RE_INT.match('-1'))
            self.assertTrue(RE_INT.match('0'))
            self.assertTrue(RE_INT.match('+0'))
            self.assertTrue(RE_INT.match('-0'))
    
            self.assertTrue(RE_INT.match('11'))
            self.assertFalse(RE_INT.match('00'))
            self.assertFalse(RE_INT.match('01'))
            self.assertTrue(RE_INT.match('+11'))
            self.assertFalse(RE_INT.match('+00'))
            self.assertFalse(RE_INT.match('+01'))
            self.assertTrue(RE_INT.match('-11'))
            self.assertFalse(RE_INT.match('-00'))
            self.assertFalse(RE_INT.match('-01'))
    
            self.assertTrue(RE_INT.match('1234567890'))
            self.assertTrue(RE_INT.match('+1234567890'))
            self.assertTrue(RE_INT.match('-1234567890'))
    
    0 讨论(0)
  • 2020-12-09 16:10

    You are apparently using Django.

    You are probably better off just using models.IntegerField() instead of models.TextField(). Not only will it do the check for you, but it will give you the error message translated in several langs, and it will cast the value from it's type in the database to the type in your Python code transparently.

    0 讨论(0)
  • 2020-12-09 16:14

    Regexp work on the character base, and \d means a single digit 0...9 and not a decimal number.

    A regular expression that matches only integers with a sign could be for example

    ^[-+]?[0-9]+$
    

    meaning

    1. ^ - start of string
    2. [-+]? - an optional (this is what ? means) minus or plus sign
    3. [0-9]+ - one or more digits (the plus means "one or more" and [0-9] is another way to say \d)
    4. $ - end of string

    Note: having the sign considered part of the number is ok only if you need to parse just the number. For more general parsers handling expressions it's better to leave the sign out of the number: source streams like 3-2 could otherwise end up being parsed as a sequence of two integers instead of an integer, an operator and another integer. My experience is that negative numbers are better handled by constant folding of the unary negation operator at an higher level.

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