tools / regex library / credit card number
regex pattern · ready to copy

Regex for matching credit card numbers (Visa / MC / Amex)

Detect the major brands by IIN prefix. Format only — DOES NOT verify Luhn.

intermediate javascript / pcre / python4 use cases

The pattern

\b(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})\b

Test cases

InputResult
4111111111111111 (Visa test)matches
5500000000000004 (MC test)matches
340000000000009 (Amex test)matches
1234567812345678 (no brand prefix)rejects
411111111111111 (15 digits, Visa needs 13/16/19)rejects

Edge cases & caveats

ALWAYS run Luhn check after regex match — regex matches valid format, not valid card. NEVER log full card numbers in production. Replace with `XXXX-XXXX-XXXX-XXXX` immediately on detection.

Common use cases

Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab  

Related

phone number · username · bitcoin address · hashtag · ↗ generate Luhn-valid test cards