UK Phone Number Regex for Python
/^(?:\+44|0)(?:\s|-)?(?:[1-9][0-9]{1,4})(?:\s|-)?(?:[0-9]{3,4})(?:\s|-)?(?:[0-9]{3,4})$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching uk 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
# UK Phone Number
# ReDoS-safe | RegexVault — Localization > Phone Numbers
import re
uk_phone_number_pattern = re.compile(r'^(?:\+44|0)(?:\s|-)?(?:[1-9][0-9]{1,4})(?:\s|-)?(?:[0-9]{3,4})(?:\s|-)?(?:[0-9]{3,4})$')
def validate_uk_phone_number(value: str) -> bool:
return bool(uk_phone_number_pattern.fullmatch(value))
# Example
print(validate_uk_phone_number("+44 20 7946 0958")) # TrueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
+44 20 7946 0958 | +1 20 7946 0958 |
020 7946 0958 | 44 20 7946 0958 |
+447911123456 | +44 (0) 20 7946 |
07911 123456 | — |
0800 123 456 | — |
020-7946-095 | — |
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
UK numbers vary in length depending on area code — London has an 8-digit local number, while rural areas may have 7 or even 6. This variability makes regex validation approximate at best.
Technical Notes
UK uses a complex numbering plan. 020 = London, 0161 = Manchester, 07xxx = mobile, 0800 = freephone, 01xxx/02xxx = geographic. The (0) is sometimes shown in international format (+44 (0)20...) but not dialed.
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