I'm learning Python and working through "Automate the boring stuff with Python". The following question is in the Regex chapter:
How would you write a regex that matches a number with commas for every three digits? It must match the following:
- '42'
- '1,234'
- '6,368,745'
but not the following:
- '12,34,567' (which has only two digits between the commas)
- '1234' (which lacks commas)
The answer in the book is as follows:
re.compile(r'^\d{1,3}(,{3})*$') #will create this regex, but other regex strings can produce a similar regular expression.
However when I try this, it does not recognise numbers such as 1,234. In the shell I type:
numRegex = re.compile(r'^\d{1,3}(,{3})*$')
And my searches produce the following:
numRegex.search('42')
<_sre.SRE_Match object; span=(0, 2), match='42'>
numRegex.search('1,234')
numRegex.search('6,368,745')
numRegex.search('1234')
numRegex.search('36')
<_sre.SRE_Match object; span=(0, 2), match='36'>
So it turns out the regex returns a result for values under 1000, but when the number is above 3 digits, comma or no, I get no result. Suggestions?
Adrian