Created
March 17, 2020 19:58
-
-
Save bigntallmike/8577f4f793cc88b92a83e2514ee1350f to your computer and use it in GitHub Desktop.
Credit Card type detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import re | |
def cctype(number): | |
cclist = { | |
'Mastercard': re.compile('^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$'), | |
'Visa': re.compile('^4[0-9]{12}(?:[0-9]{3})?$'), | |
'American Express': re.compile('^3[47][0-9]{13}$'), | |
'Diners Club': re.compile('^3(?:0[0-5]|[68][0-9])[0-9]{11}$'), | |
'Discover': re.compile('^6(?:011|5[0-9]{2})[0-9]{12}$') | |
} | |
for cc in cclist: | |
if cclist[cc].match(number): | |
return cc | |
return None | |
import sys | |
if __name__ == "__main__": | |
'''Specify credit card on command-line to get type''' | |
print(str(cctype(sys.argv[1]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Special thanks to https://www.regexbuddy.com/library.html