REGEXVAULTv2.0
Identity & PII/Financial PII
Verified Safe

Credit Card Number with Network Detection Regex for Python

/^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching credit card number with network detection, ported and verified for Python. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Python project — whether you're validating in a Django view, a FastAPI endpoint, or a standalone data processing script.

Python Implementation

Python
# Credit Card Number with Network Detection
# ReDoS-safe | RegexVault — Identity & PII > Financial PII

import re

credit_card_number_with_network_detection_pattern = re.compile(r'^(?:(4)[0-9]{12}(?:[0-9]{3,6})?|(5[1-5][0-9]{14}|2(?:2[2-9][1-9]|[3-6][0-9]{2}|7[01][0-9]|720)[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])[0-9]{11})|(6(?:011|5[0-9]{2})[0-9]{12,15})|((?:2131|1800|35[0-9]{3})[0-9]{11}))$')

def validate_credit_card_number_with_network_detection(value: str) -> bool:
    return bool(credit_card_number_with_network_detection_pattern.fullmatch(value))

# Example
print(validate_credit_card_number_with_network_detection("4111111111111111"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
41111111111111111234567890123456
5500005555555559411111111111
378282246310005411111111111111111111
30569309025904
6011111111111117

When to use this pattern

This pattern is drawn from the Identity & PII > Financial PII category and carries a ReDoS-safe certification. That matters for Python developers because particularly important in Python web servers where CPU-bound regex operations can stall concurrent request handling. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

PCI-DSS requires Luhn validation, not just format matching. Storage of card data requires quarterly security assessments and annual PCI-DSS certification. Use a payment gateway (Stripe, Adyen) to avoid handling raw PANs.

Technical Notes

Capture groups: 1=Visa, 2=Mastercard, 3=Amex, 4=Diners Club, 5=Discover, 6=JCB. PCI-DSS: never store CVV, never log PANs, always encrypt stored card data, use tokenization for recurring billing.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.

Submit a Pattern