Comment: 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.
Negative Lookbehind(?<!\d) Comment: s - Short [less than 16 digits], m - Medium [16 digits], l - Long [more than 16 digits; not yet implemented]
Comment: First four digits
Non-Capturing Group(?:
(?# 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}))
) 1st Alternative
(?# 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}
) Comment: Standard 16 digit numbers
Group m(?<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}
) 1st Alternative
(?#MasterCard between 222100-272099)
222[1-9] Comment: MasterCard between 222100-272099
1-9one character from 1 (4910) to 9 (5710)
3rd Alternative2[3-6]\d{2} 2the literal 2 (5010 / 628 / 3216)
3-6one character from 3 (5110) to 6 (5410)
\d{2}a digit, repeated exactly 2 times
0the literal 0 (4810 / 608 / 3016)
1the literal 1 (4910 / 618 / 3116)
\da digit
2720the literal text 2720 6th Alternative
(?#JCB is between 3528-3589)
352[89] Comment: JCB is between 3528-3589
8the literal 8 (5610 / 708 / 3816)
9the literal 9 (5710 / 718 / 3916)
3-8one character from 3 (5110) to 8 (5610)
\da digit
8th Alternative
(?#Visa, MasterCard, Discover)
[4-6]\d{3}
Comment: Visa, MasterCard, Discover
4-6one character from 4 (5210) to 6 (5410)
\d{3}a digit, repeated exactly 3 times
2nd Alternative
(?#American Express 15 digits | Diners Club 14 digits)
(?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2}))
Comment: American Express 15 digits | Diners Club 14 digits
Group s(?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2})) 1st Alternative(?<ax>3[47]\d{2}) Group ax(?<ax>3[47]\d{2}) 3the literal 3 (5110 / 638 / 3316)
4the literal 4 (5210 / 648 / 3416)
7the literal 7 (5510 / 678 / 3716)
\d{2}a digit, repeated exactly 2 times
2nd Alternative(?<dc>30[0-5]\d|36\d{2}) Group dc(?<dc>30[0-5]\d|36\d{2}) Comment: Rest of the card number
Condition(?(<m>)\d{4}(?(<del>)\k{del})\d{4}(?(<del>)\k{del})\d{4}|(?:\d{6})(?(<del>)\k{del})(?(<ax>)\d{5}|\d{4})) Comment: END
g modifier: global. Finds all matches instead of stopping after the first
m modifier: multiline. Causes ^ and $ to match the start and end of each line, not only the start and end of the string
x modifier: extended. Ignores unescaped whitespace and comments outside character classes