Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Created March 17, 2020 19:58
Show Gist options
  • Save bigntallmike/8577f4f793cc88b92a83e2514ee1350f to your computer and use it in GitHub Desktop.
Save bigntallmike/8577f4f793cc88b92a83e2514ee1350f to your computer and use it in GitHub Desktop.
Credit Card type detection
#!/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])))
@bigntallmike
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment