Qaynaq LABS · TOOLS

REGEX · PYTHON

Regex explained

^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|35(?:0[0-5]|[68][0-9])[0-9]{11})$

Matches valid credit card numbers for Visa, MasterCard, Amex, and Discover using their respective number patterns.

Token-by-token breakdown

TokenMeaningExample
^ Anchor: asserts position at start of the string Start of string
(?: Non-capturing group start Start of group
4 Literal digit 4 (Visa card prefix) 4
[0-9]{12} Exactly 12 digits (0-9) 123456789012
(?: Non-capturing group start Start of optional group
[0-9]{3} Exactly 3 digits (0-9) 345
)? End of group, quantifier: 0 or 1 occurrence Optional 3 digits
| Alternation (OR operator) OR
5 Literal digit 5 (MasterCard prefix) 5
[1-5] Digit in range 1-5 (MasterCard second digit) 1
[0-9]{14} Exactly 14 digits (0-9) 23456789012345
| Alternation (OR operator) OR
3 Literal digit 3 (Amex or Discover prefix) 3
[47] Digit 4 or 7 (Amex second digit) 4
[0-9]{13} Exactly 13 digits (0-9) 4567890123456
| Alternation (OR operator) OR
35 Literal digits 3 and 5 (Discover prefix) 35
(?: Non-capturing group start Start of group
0[0-5] Digit 0 followed by 0-5 05
| Alternation (OR operator) OR
[68][0-9] Digit 6 or 8 followed by any digit 60
) End of non-capturing group End of group
[0-9]{11} Exactly 11 digits (0-9) 12345678901
) End of non-capturing group End of group
$ Anchor: asserts position at end of the string End of string

Matches

  • 4111111111111111
  • 4222222222222
  • 5105105105105100
  • 5555555555554444
  • 340000000000009
  • 378282246310005
  • 3530111333300000
  • 3566002020360505

Does not match

  • 411111111111111
  • 42222222222222222
  • 5005105105105100
  • 6011111111111117
  • 330000000000009
  • 388282246310005
  • 3630111333300000
  • 353011133330000

Common pitfalls

  • Forgetting to anchor with ^ and $, allowing partial matches in longer strings.
  • Assuming this matches all valid credit card numbers (e.g., Diners Club, JCB are missing).
  • Not accounting for spaces or hyphens in input (this pattern requires raw digits only).
  • Misinterpreting the optional group for Visa cards (e.g., 4 digits total 13 or 16).
  • Overlooking that Amex numbers are 15 digits (this pattern matches 15-digit Amex numbers).

Performance notes

The pattern is well-structured with no nested quantifiers or backtracking risks. The alternation is linear and efficient. No catastrophic backtracking expected. Anchoring with ^ and $ ensures full-string matching.

About regular expressions

Regular expressions are tiny programs that match patterns in text. They're powerful and notoriously cryptic — most engineers can write them but few can read them confidently. Qaynaq Labs' regex explainer breaks any pattern into its constituent tokens, shows what each one does, and runs through both matching and non-matching examples so you can verify the logic.

Different regex flavors (JavaScript, PCRE, Python, Go) have subtle differences in syntax and supported features. Pick your flavor in the input — the explainer accounts for it.

Frequently asked

Does this pattern validate Luhn checksum?

No, this pattern only validates the number format and prefix ranges. Luhn checksum validation requires additional logic.

Can this pattern match credit card numbers with spaces or hyphens?

No, this pattern expects raw digits only. Preprocessing (removing non-digit characters) is required for such inputs.

Why does the Visa pattern have an optional group?

Visa cards can be 13 or 16 digits long. The pattern matches 4 followed by 12 digits (13 total) or 4 followed by 12 digits plus 3 optional digits (16 total).

Does this pattern match all Discover card numbers?

Yes, Discover cards start with 6011 or 65, and this pattern covers 35 with specific ranges for Discover numbers.