Brazilian Phone Number Regex for Python
/^(?:\+55\s?)?(?:\(?([1-9][1-9])\)?\s?)(?!\1\s?9[0-9]{8}$|11\s?987654321$)(?:9[0-9]{4}-[0-9]{4}|[2-9][0-9]{3}-[0-9]{4}|9[0-9]{8}|[2-9][0-9]{7})$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching brazilian phone number, ported and verified for Python. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
# Brazilian Phone Number
# ReDoS-safe | RegexVault — Localization > Phone Numbers
import re
brazilian_phone_number_pattern = re.compile(r'^(?:\+55\s?)?(?:\(?([1-9][1-9])\)?\s?)(?!\1\s?9[0-9]{8}$|11\s?987654321$)(?:9[0-9]{4}-[0-9]{4}|[2-9][0-9]{3}-[0-9]{4}|9[0-9]{8}|[2-9][0-9]{7})$')
def validate_brazilian_phone_number(value: str) -> bool:
return bool(brazilian_phone_number_pattern.fullmatch(value))
# Example
print(validate_brazilian_phone_number("+55 11 98765-4321")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
+55 11 98765-4321 | +55 01 98765-4321 |
(11) 98765-4321 | 11 12345-6789 |
11987654321 | +55 11 12345-6789 |
+5511912345678 | — |
(21) 3456-7890 | — |
11 987654321 | — |
When to use this pattern
This pattern is drawn from the Localization > Phone 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
The extra 9 digit for mobiles was added region by region between 2012-2016. Old data may have 8-digit mobile numbers in some DDDs. Validate based on the DDD's transition date for historical data.
Technical Notes
Brazilian DDD codes are 2 digits (11=São Paulo, 21=Rio de Janeiro, etc.). Mobile numbers have 9 digits starting with 9; landlines have 8 digits. +55 is the country code. All mobiles received a 9th digit in 2012-2016.
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