REGEXVAULTv2.0
Identity & PII/National Identity Numbers
Verified Safe

Brazilian CPF (Cadastro de Pessoas Físicas) Regex for Python

/^(?!(\d)\1{10})(?:\d{3}\.\d{3}\.\d{3}-\d{2}|\d{11})$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching brazilian cpf (cadastro de pessoas físicas), 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
# Brazilian CPF (Cadastro de Pessoas Físicas)
# ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers

import re

brazilian_cpf_cadastro_de_pessoas_fsicas_pattern = re.compile(r'^(?!(\d)\1{10})(?:\d{3}\.\d{3}\.\d{3}-\d{2}|\d{11})$')

def validate_brazilian_cpf_cadastro_de_pessoas_fsicas(value: str) -> bool:
    return bool(brazilian_cpf_cadastro_de_pessoas_fsicas_pattern.fullmatch(value))

# Example
print(validate_brazilian_cpf_cadastro_de_pessoas_fsicas("123.456.789-09"))  # True

Test Cases

Matches (Valid)
Rejects (Invalid)
123.456.789-09123.456.789-0
12345678909123.456.7890-9
987.654.321-00
12345678900
111.111.111-11

When to use this pattern

This pattern is drawn from the Identity & PII > National Identity Numbers 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

Must validate the check digits — all-same-digit numbers (000.000.000-00 through 999.999.999-99) pass the format check but are explicitly invalid. Use a CPF validation library.

Technical Notes

CPF is 11 digits with two check digits computed via weighted sum. All-same-digit CPFs (111.111.111-11) are structurally valid but officially invalid. Check digits: first is 10-(sum × weights 10-2 mod 11), second uses the same method with weights 11-2.

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