import re
regex = re.compile(r"""
^
(?:4[0-9]{12}(?:[0-9]{3})?
|
5[1-5][0-9]{14}
|
6(?:011|5[0-9][0-9])[0-9]{12}
|
3[47][0-9]{13}
|
3(?:0[0-5]|[68][0-9])[0-9]{11}
|
(?:2100|2131|1800|35\d{3})\d{11})
$
""", flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE)
test_str = ("#American Express\n"
"370517943574132\n"
"372714451742486\n"
"370010255141385\n"
"341263547614307\n"
"343874494387669\n\n"
"#VISA\n"
"4024007125780444\n"
"4439944519233615\n"
"4658355677043536\n"
"4532926168018906\n"
"4532249806735728\n\n"
"#MasterCard\n"
"5524097521691644\n"
"5367170623993901\n"
"5553103980950937\n"
"5549194987582424\n"
"5141794881796756\n\n"
"#JCB 15 digits\n"
"180078244412845\n"
"210013400722277\n"
"210082510016250\n"
"180056142071970\n"
"210043823226606\n\n"
"#JCB\n"
"3158822586903214\n"
"3088687202983378\n"
"3158899851849561\n"
"3096803356450490\n"
"3337852908456769\n\n"
"#Dinners Club\n"
"30193567772121\n"
"30131361923813\n"
"30198560976769\n"
"30260244203356\n"
"36297440059376\n\n\n")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html