Token-by-token breakdown
| Token | Meaning | Example |
|---|---|---|
^ |
Anchor: asserts the start of the string | Start of '4111111111111111' |
(?: |
Non-capturing group start: groups alternatives without capturing | Start of Visa pattern |
4 |
Literal digit 4: matches the first digit of Visa cards | First digit in '4111111111111111' |
[0-9]{12} |
Exactly 12 digits (0-9): matches the next 12 digits of Visa cards | Digits 2-13 in '4111111111111111' |
(?: |
Non-capturing group start: groups optional CVV | Start of optional CVV for Visa |
[0-9]{3} |
Exactly 3 digits (0-9): matches the CVV for Visa | Last 3 digits in '4111111111111111123' |
)? |
Quantifier: makes the preceding group optional (0 or 1 times) | Optional CVV in '4111111111111111' |
| |
Alternation: separates alternative patterns | Separator between Visa and MasterCard patterns |
5 |
Literal digit 5: matches the first digit of MasterCard cards | First digit in '5500000000000004' |
[1-5] |
Digit class: matches digits 1-5 (second digit of MasterCard) | Second digit in '5500000000000004' |
[0-9]{14} |
Exactly 14 digits (0-9): matches the remaining 14 digits of MasterCard | Digits 3-16 in '5500000000000004' |
| |
Alternation: separates alternative patterns | Separator between MasterCard and Amex patterns |
3 |
Literal digit 3: matches the first digit of American Express cards | First digit in '378282246310005' |
[47] |
Digit class: matches digits 4 or 7 (second digit of Amex) | Second digit in '378282246310005' |
[0-9]{13} |
Exactly 13 digits (0-9): matches the remaining 13 digits of Amex | Digits 3-15 in '378282246310005' |
| |
Alternation: separates alternative patterns | Separator between Amex and Discover patterns |
3 |
Literal digit 3: matches the first digit of Discover cards | First digit in '30569309025904' |
(?: |
Non-capturing group start: groups second digit options for Discover | Start of second digit group for Discover |
0[0-5] |
Digit class: matches 0 followed by 0-5 (second and third digits) | Digits 2-3 in '30569309025904' |
| |
Alternation: separates alternative second/third digit patterns | Separator between 0[0-5] and [68][0-9] |
[68][0-9] |
Digit class: matches 6 or 8 followed by any digit (second and third digits) | Digits 2-3 in '38520000023237' |
) |
Non-capturing group end: ends the second digit options group | End of second digit group for Discover |
[0-9]{11} |
Exactly 11 digits (0-9): matches the remaining 11 digits of Discover | Digits 4-14 in '30569309025904' |
) |
Non-capturing group end: ends the main alternation group | End of all card pattern alternatives |
$ |
Anchor: asserts the end of the string | End of '4111111111111111' |
Matches
4111111111111411111111111112355000000000000043782822463100053056930902590438520000023237
Does not match
4111111111115500000000000003782822463100030569309025903852000002323123456789012345641111111111111111234
Common pitfalls
- This regex does not validate the Luhn algorithm (checksum) — it only checks the card number format.
- The optional CVV for Visa (3 digits) is not part of the standard card number format — this may cause false positives.
- The regex does not account for spaces or hyphens in card numbers (e.g., '4111 1111 1111 1111').
- It matches invalid card numbers that pass the format check but fail the Luhn check.
Performance notes
The regex is well-structured with no catastrophic backtracking risks due to the use of non-capturing groups and quantifiers. However, anchoring (^ and $) is critical to prevent partial matches. The alternation is ordered from most common to least common patterns, which can improve matching speed in practice.