Regular Expressions 101

Community Patterns

Credit Card Numbers

0

Regular Expression
PCRE (PHP <7.3)

/
(?# This regex was created for the purposes of detecting potentially compromised credentials for the purposes of information security and not to be used for malicious purposes.) (?<!\d) (?# s - Short [less than 16 digits], m - Medium [16 digits], l - Long [more than 16 digits; not yet implemented]) (?# First four digits) (?: (?# Standard 16 digit numbers) (?<m> (?#MasterCard between 222100-272099) 222[1-9]|223\d|2[3-6]\d{2}|27[01]\d|2720| (?#JCB is between 3528-3589) 352[89]|35[3-8]\d| (?#Visa, MasterCard, Discover) [4-6]\d{3} )| (?#American Express 15 digits | Diners Club 14 digits) (?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2})) ) (?# Rest of the card number) (?<del>\D+)?(?(<m>)\d{4}(?(<del>)\k{del})\d{4}(?(<del>)\k{del})\d{4}|(?:\d{6})(?(<del>)\k{del})(?(<ax>)\d{5}|\d{4})) (?#END) (?!\d)
/
gmx

Description

Regex to detect credit card numbers.

This regex was created for the purposes of detecting potentially compromised credentials for the purposes of information security and not to be used for malicious purposes.

Based on data from https://www.freeformatter.com/credit-card-number-generator-validator.html

Currently only works for Visa, MasterCard, Discover, AmEx, JCB, Diners Club and only 16 digits (15 for AmEx, 14 for Diners Club Int'l)

Submitted by anonymous - 4 years ago