Я работаю с многострочной строкой, пытаясь захватить в строке действительные числа, разделенные запятыми.
Например:
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
"""
В идеале я хочу re.findall вернуть:
['42', '1,234', '3,456,780']
Вот мой код:
a = """
42
1,234
3,456,780
34,56,780
1234
"""
regex = re.compile(r'\d{1,3}(?:,\d{3})*')
print(regex.findall(a))
Результат с моим кодом выше:
['42', '1,234', '3,456,780', '34', '56,780', '123', '4']
Но мой желаемый результат должен быть:
['42', '1,234', '3,456,780']