Regex Problem
So whilst working on my Python, I stumbled upon this rather tricky Regex problem that took me a long time to solve. The problem is as follows: 1 2 3 4 5 6 7 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 difficult part of this problem is making sure that your regex manages to match everything under 1000, which dictates something like \d{1-3}, but at the same time, this logic fades into irrelevance once the number passes 1000 for instance in the case of '1,234'. A thought I had was: how do you include conditional logic into Regex itself? Like, if the number is more than 1000, then the RHS parts of the regex cannot be 1-3 digits, but must be \d{3}. And so, I tried incorporating conditional logic into my regex like so, the final product of which was: ...