I'm working with a multi-line string, trying to capture valid comma separated numbers in the string.
For example:
my_string = """42 <---capture 42 in this line
1,234 <---capture 1,234 in this line
3,456,780 <---capture 3,456,780 in this line
34,56,780 <---don't capture anything in this line but 34 and 56,780 captured
1234 <---don't capture anything in this line but 123 and 4 captured
"""
Ideally, I want re.findall to return:
['42', '1,234', '3,456,780']
Here are my code:
a = """
42
1,234
3,456,780
34,56,780
1234
"""
regex = re.compile(r'\d{1,3}(?:,\d{3})*')
print(regex.findall(a))
The result with my code above is:
['42', '1,234', '3,456,780', '34', '56,780', '123', '4']
But my desired output should be:
['42', '1,234', '3,456,780']
['42', '1,234', '3,456,780']), what do you mean by, "...but 34 and 56,780 captured" and "...but 123 and 4 captured"?