As Greg said, regexes are not the right tool for the job here. But if you insist on knowing how the regex you pasted works:
The most important thing to remember is that 2**31 - 1 = 2147483647
(a number with 10 digits). In essence, the regex says:
- The number can have 1-9 digits, OR
- It can be 1 with any 9 digits after it, OR
- 20 with any 8 digits after it, OR
- 213 with any 7 digits after it, OR
- ... I'm sure you see where it's going
It restricts the numbers to the range of being below 2147483647.
P.S. given such a number as a string s
, in Python, you can just pose this condition:
1 <= int(s) <= 2**31 - 1