# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
(?(DEFINE)
(?<separator>\D?)
(?<iin> # issuer identification number for 16 and 19 digit cards
# MIR
220[0-4]
# Mastercard
| 222[1-9]
| 22[3-9]\d
| 2[3-6]\d{2}
| 27[01]\d
| 2720
| 5[1-5]\d{2}
# JCB
| 352[89]
| 35[3-8]\d
# Visa
| 4\d{3}
# Dankort
| 5019
# Discover
| 6011
| 622[1-9]
| 64[4-9]\d
| 65\d{2}
# InterPayment
| 63[7-9]\d
# UnionPay
| 62\d{2}
# Maestro
| 5[06-9]\d{2}
| 6\d{3}
# Troy
| 9792
)
)
## pattern begins here
\b
(?|
(?: # 14 digits: XXXX-XXXXXX-XXXX
# Diners Club
(?:
30[0-5]\d
| 36\d{2}
)
(?<sep>(?&separator))
\d{6}
\k<sep>
\d{4}
)
| (?: # 15 digits: XXXX-XXXXXX-XXXXX
# American Express
3[47]\d{2}
(?<sep>(?&separator))
\d{6}
\k<sep>
\d{5}
)
| (?: # 16 digits: XXXX-XXXX-XXXX-XXXX
(?&iin)
(?<sep>(?&separator))
\d{4}
\k<sep>
\d{4}
\k<sep>
\d{4}
)
| (?: # 19 digits: XXXXXXXXXXXXXXXXXXX
(?&iin)
\d{15}
)
)
\b
"""
test_str = ("VISA:\n"
"4916-0964-6896-0680,4716 4897 5598 3394 4024007191458214;4929677677283453911\n"
"MasterCard:\n"
"5324-3523-3637-8594 2221 0059 1411 2881 2720993802486259\n"
"American Express (AMEX):\n"
"3417-364760-63463\n"
"3703,747943,64738\n"
"345620254969461\n"
"Discover:\n"
"6011;1100;7456;3157\n"
"6011|3448|8657|7515\n"
"6011549504486287170\n"
"JCB:\n"
"3535#1626#1193#3330\n"
"3537w2194w6345w7160\n"
"3544115541621080497\n"
"Diners Club - North America:\n"
"5550@6773@7664@9628\n"
"5498!9120!2137!8785\n"
"5418926330224156\n"
"Diners Club - Carte Blanche:\n"
"3038~338630~9862\n"
"3043 979928 4843\n"
"30514730539784\n"
"Diners Club - International:\n"
"3601-529450-0216\n"
"3684 740236 6092\n"
"36068539661053\n"
"Maestro:\n"
"5038-1482-6826-8317\n"
"6761 1161 5463 2772\n"
"5020976162959049\n"
"Visa Electron:\n"
"4175-0064-3983-6063\n"
"4844 2214 1640 8361\n"
"4508772794670888\n"
"InstaPayment:\n"
"6375-9237-0580-9744\n"
"6387 4000 3155 2203\n"
"6387679822589033\n"
"UnionPay:\n"
"6257-8903-9875-9366\n"
"6283348562159853\n"
"6234512468972678451\n\n"
"Failed Luhn:\n"
"4024007191458216\n"
"4929677677283453917\n"
"Unrecognized IIN:\n"
"7135865572496322\n"
"3719865572496325\n"
"0139865572496321\n"
"7019865572496321\n"
"Improper spacing:\n"
"4024-0071 9145-8214\n"
"4024-00719145-8214\n"
"4024-007-19145-8214\n"
"3051-4730-5397-84\n"
"40240071914582144024007191458214\n"
"Unexpected length:\n"
"34562025496943\n"
"3498348562489555\n"
"305147305397847\n"
"3051473053978442\n"
"498348562489551\n"
"49834856248955837\n"
"498348562489558367\n"
"49834856248955836445")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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