0

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

adrian
  • 7
  • 1

1 Answers1

-2

If you want to match numbers with a thousands delimiter, you want to fix a typo in your regex:

re.compile(r'^\d{1,3}(,\d{3})*$')

Now, if you want to also match numbers without comas, you need to make it optional as well:

re.compile(r'^\d{1,3}(,?\d{3})*$')

A faster version without capturing groups (faster):

re.compile(r'^\d{1,3}(?:,?\d{3})*$')
vdust
  • 65
  • 3